What return type should be used for setTimeout in TypeScript?

You can use window.setTimeout it returns a type of number.

let a: number;
a = window.setTimeout(function() {}, 0);

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.


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. VS Code 2 type definitions of setTimeout()

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

Workaround:

  • Use window.setTimeout() instead

Tags:

Typescript