Skip to content

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

Open
Martin Ferret (MartinFerret) wants to merge 1 commit into
microsoft:mainfrom
MartinFerret:fix-2084-generic-posttask
Open

Martin Ferret (MartinFerret) wants to merge 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.

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

1 participant