In the lib.d.ts
file, the types for Window.setTimeout
are defined as:
declare function setTimeout(
handler: TimerHandler,
timeout?: number,
...arguments: any[]
): number;
I am interested in extending these types so that the arguments
type is inferred from the handler rather than being an any[]
An example of the desired extension would be:
declare global {
interface Window {
setTimeout<THandler extends (...args: any[]) => any>(
handler: THandler,
timeout?: number,
...arguments: Parameters<THandler>
): number;
}
}
This approach works but requires manual specification of the generic THandler
type.
For instance:
declare function doSomething(value: number): number;
setTimeout(
doSomething,
100,
'john', // no error is flagged by the compiler regarding this string argument
);
setTimeout<typeof doSomething>(
doSomething,
100,
'john', // expected error is detected by the compiler
);
Is there a method to have setTimeout
automatically infer the THandler
argument based on the passed handler?