-
Notifications
You must be signed in to change notification settings - Fork 12.8k
setTimeout defined in lib.dom.d.ts is not type safe #32186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Is the reason you're expecting this to fail because you've passed extra parameters to |
I suspect the OP expects that setTimeout(callback: (foo: number) => void, timeout: number, foo: number): number; The DOM signatures are based off the WebIDL, which wouldn't be able to model the relationship between the callback signature and the arguments passed to |
Oh, I see, the declare function setTimeout<A extends any[]>(callback: (...args: A) => void, timeout: number, ...args: A): number; |
@fatcerberus Thanks for the signature and it works. But there is still a problem that, it breaks the ability that declare function setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;
type TimerHandler = string | Function; Btw, this does not only apply to declare function setInterval<A extends any[]>(callback: (...args: A) => void , timeout?: number, ...args: A): number; |
declare function setTimeout<A extends any[]>(handler: (...args: A) => void, timeout?: number, ...arguments: A): number;
declare function setTimeout(handler: string, timeout?: number, ...arguments: any[]): number;
declare function setInterval<A extends any[]>(handler: (...args: A) => void, timeout?: number, ...arguments: A): number;
declare function setInterval(handler: string, timeout?: number, ...arguments: any[]): number; That will do it. So another question: |
There's a PR for this on the TSJS-lib-generator repo: microsoft/TypeScript-DOM-lib-generator#810 Unfortunately, it was closed. |
This is still a problem even though the underlying root cause seems to have been addressed. function setTimeout<TArgs extends any[]>(
callback: (...args: TArgs) => void,
ms?: number,
...args: TArgs
) {
console.log(ms)
callback(...args)
}
function foo<TArgs extends any[]>(
callback: (...args: TArgs) => void,
ms?: number,
...args: TArgs
) {
console.log(ms)
callback(...args)
}
function callback(bar: string) {
console.log(bar);
}
foo(callback, 0); // Error - as expected
setTimeout(callback, 0); // No error This illustrates the typescript is now capable of correctly checking the type but for some reason, it doesn't do it for |
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 3.5.2
Search Terms:
settimeout type safe
Code
Expected behavior:
It shall not pass thanks to the type error.
Actual behavior:
It passes.
Playground Link:
https://www.typescriptlang.org/play/#code/MYewdgzgLgBAZjAvDAFHEIBcMwFcC2ARgKYBOAlEgHwyiQgA2xAdAyAOZoYwDUMAjOQDcAKAjEoAFQCW+YiFxQ0AGhgAGVQHIAhoULBgAE0PFNWwpuFA
Related Issues:
emm, maybe not.
The text was updated successfully, but these errors were encountered: