Skip to content
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

Fix setTimeout/setInterval/setImmediate functions #19635

Open
falsandtru opened this issue Nov 1, 2017 · 6 comments
Open

Fix setTimeout/setInterval/setImmediate functions #19635

falsandtru opened this issue Nov 1, 2017 · 6 comments
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Milestone

Comments

@falsandtru
Copy link
Contributor

Fix too lax typings.

TypeScript Version: master

Expected behavior:

declare function setTimeout(handler: (...args: any[]) => void, timeout?: number, ...args: any[]): number;

Actual behavior:

declare function setTimeout(handler: (...args: any[]) => void, timeout: number): number;
declare function setTimeout(handler: any, timeout?: any, ...args: any[]): number;
@mhegazy
Copy link
Contributor

mhegazy commented Nov 1, 2017

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 1, 2017
@falsandtru
Copy link
Contributor Author

Then this is valid, right?

declare function setTimeout(handler: ((...args: any[]) => void) | string, timeout?: number, ...args: any[]): number;

@aluanhaddad
Copy link
Contributor

aluanhaddad commented Nov 2, 2017

Be aware, that if you pass a string it is equivalent to eval.

Douglas Crockford warned of this.

Perhaps this second declaration could go into @RyanCavanaugh's proposed Annex B 😉

@mhegazy mhegazy added Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Bug A bug in TypeScript Help Wanted You can do this and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Nov 2, 2017
@mhegazy mhegazy added this to the Community milestone Nov 2, 2017
@berickson1
Copy link

berickson1 commented Jan 10, 2019

Looking at some of the new features that Typescript provides, I'd expect the following to be a better more specific definition

declare function setTimeout<T>(handler: string | ((...args: T[]) => void), timeout?: number , ...params: T[]) => number;

@xaviergxf
Copy link

Hi, no news about this?

@Jack-Works
Copy link
Contributor

We hit this problem. TypeScript does not check this code:

function f(x: number) {
  x.toExponential()
}

setTimeout(f, 100)
//         ~ should be an error!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Help Wanted You can do this
Projects
None yet
Development

No branches or pull requests

7 participants