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

setTimeout incorrect typings browser #32133

Closed
GoGoris opened this issue Jun 27, 2019 · 2 comments
Closed

setTimeout incorrect typings browser #32133

GoGoris opened this issue Jun 27, 2019 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@GoGoris
Copy link

GoGoris commented Jun 27, 2019

TypeScript Version: 3.4.5

Search Terms: setTimeout

Code

const timeout: number = setTimeout(() => console.log('anything'), 300);

Expected behavior:

It compiles correctly

Actual behavior:

Type 'Timeout' is not assignable to type 'number'.

1 const timeout: number = setTimeout(() => console.log('anything'), 300);
        ~~~~~~~


Found 1 error.

Playground Link:

Not reproducable in playground

Related Issues:

Possibly #19635, but this issue seems to be outdated because now the declaration of setTimeout looks like:

type TimerHandler = string | Function;
setTimeout(handler: TimerHandler, timeout?: number, ...arguments: any[]): number;

https://stackoverflow.com/q/45802988

@GoGoris GoGoris changed the title Incorrect typings for setTimeout in the browser setTimeout incorrect typings browser Jun 27, 2019
@nmain
Copy link

nmain commented Jun 27, 2019

Somewhere in your dependency tree is a package that works in both Node and the browser. When used in Node, it probably supports some Node types like Buffer, and so it brings in @types/node to express that relationship, which causes Node globals to become available, and Node has a setTimeout with a different definition than the browser one.

As a workaround, you can use window.setTimeout which will only have the browser definition.

I think the best solution to this is to move forward with #31894 and then change typings of all packages that optionally use Node to support them, but that's a ways off.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Jun 27, 2019
@AviVahl
Copy link

AviVahl commented Jun 29, 2019

I find using ReturnType<typeof setTimeout> instead of number a bit cleaner workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants