What return type should be used for setTimeout in TypeScript?
Consider following code:
const timer: number = setTimeout(() => '', 1000);
Typescript throws an error: Type 'Timer' is not assignable to type 'number'.
Quick lookup tells me that setTimeout
returns NodeJS.Timer
.
But if I am doing browser-based development, using NodeJS.Timer
feels wrong. Which is the correct type definition or return type for making setTimeout
work without resorting to any
declaration?
Simplest solution is to allow type inference to work and not specify any type at all. If you need to specify a type, seeing as the type is not consistent between the browser and node declarations, you could use ReturnType
to specify that the type of the variable is whatever the return type of setTimeout
is:
const timer: ReturnType<typeof setTimeout> = setTimeout(() => '', 1000);
Alternately, window.setTimeout
can also be used instead of just setTimeout
. It returns proper return type.
You can use window.setTimeout
it returns a type of number
.
let a: number;
a = window.setTimeout(function() {}, 0);
This happens because Typescript will search for all type definitions under node_modules/@types
If you installed NodeJS type definition (comes with many npm packages) into ~/node_modules/@types/node/globals.ts
and your project is in ~/Projects/myproject
, the NodeJS definitions for setTimeout
will leak.
By default all visible “@types” packages are included in your compilation. Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on.
See: https://www.typescriptlang.org/tsconfig#types
If Typescript finds custom type definitions its is prioritized over the default type definitions.
Solutions:
- Specify compilerOption which paths to search for type definitions:
"typeRoots":[]
- Specify compilerOption which type definitions to load from the default paths:
"types": []
- Remove the definitions file from the default search paths
- Use
window.setTimeout()
instead
window.setTimeout
returns number
. Ideally, you want to define your own type to distinguish it from number (and prevent some ops like +
which don't make sense for timers).
type TimerHandle = number;
For anyone else having this error what worked for me is adding in the tsconfig.js file:
"compilerOptions": {
...
"types": [],
}