Typescript: What is the correct type for a Timeout?
In a Web Browser app, setTimeout
returns a number
.
This setup requires the DOM
library (or equivalent).
In a NodeJS app, setTimeout
returns a NodeJS.Timeout
.
npm install @types/node --save-dev`.
For this case, I will give you 2 options:
- Define type of
debounceResize
as return type ofsetTimeout
, useReturnType
native generic.
let debounceResize: ReturnType<typeof setTimeout>;
- Use
window.setTimeout
instead of onlysetTimeout
.window.setTimeout
returns a number.
let debounceResize: number;
debounceResize = window.setTimeout(() => {}, 1000);