Skip to content

Make Scheduler.postTask generic over the callback result - #2583

Merged
github-actions[bot] merged 1 commit into
microsoft:mainfrom
MartinFerret:fix-2084-generic-posttask
Nov 8, 2026
Merged

github-actions[bot] merged 1 commit into
microsoft:mainfrom
MartinFerret:fix-2084-generic-posttask

Conversation

@MartinFerret

Copy link
Copy Markdown
Contributor

Fixes #2084

Scheduler.postTask() currently returns Promise<any> and its callback is typed SchedulerPostTaskCallback with signature (): any, so the type of the callback's result is lost.

This makes both generic, through inputfiles/overridingTypes.jsonc only:

interface SchedulerPostTaskCallback<T = any> {
    (): T | PromiseLike<T>;
}
postTask<T = any>(callback: SchedulerPostTaskCallback<T>, options?: SchedulerPostTaskOptions): Promise<T>;

The callback returns T | PromiseLike<T> because postTask() resolves its promise with the callback's result, so an async callback is unwrapped: postTask(async () => "done") gives Promise<string>. This follows the existing TransformerFlushCallback pattern. T defaults to any, like structuredClone<T = any>, so references to SchedulerPostTaskCallback without a type argument keep compiling.

Behavior change: the result type is now inferred from the callback instead of any. The code that relied on the any result (for example assigning await scheduler.postTask(() => 42) to a string) now gets a type error. That is the only change.

Added unittests/files/schedulerPostTask.ts which checks the result types exactly and fails with the override reverted.

Hope this is ok! 😊

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR!

This section of the codebase is owned by Kagami Sascha Rosylight (@saschanaz) - if they write a comment saying "LGTM" then it will be merged.

(<T>() => T extends A ? 1 : 2) extends <T>() => T extends B ? 1 : 2
? true
: false;
declare function assertExact<T extends true>(): void;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are variations of this already in other tests, we should have a shared definition...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right about the shared definition. I'd be happy to follow up with a PR going that way, are you OK with that?

@saschanaz

Copy link
Copy Markdown
Contributor

Looks reasonable, thanks! LGTM

@github-actions
github-actions Bot merged commit a3f7c80 into microsoft:main Nov 8, 2026
8 checks passed
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown
Contributor

Merging because Kagami Sascha Rosylight (@saschanaz) is a code-owner of all the changes - thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Web API type definition issue] Missed SchedulerPostTaskCallback payload type parameter

2 participants