"TS2322: Type 'Timeout' is not assignable to type 'number'" when running unit tests "TS2322: Type 'Timeout' is not assignable to type 'number'" when running unit tests typescript typescript

"TS2322: Type 'Timeout' is not assignable to type 'number'" when running unit tests


You could try with using window.setTimeout instead of just setTimeout, this way the typescript one will be explicitly used


You can use:

let timeoutId: null | ReturnType<typeof setTimeout> = null...timeoutId = setTimeout(...)

It'll pick correct declaration depending on your context.

I'm seeing this discrepency when using vscode/tsc (NodeJS.Timeout) and running ts-jest (number). This is the only way the whole thing typechecks on both sides.


You can use something like:

let myTimeOut: NodeJS.Timeout | null = null;myTimeOut = setTimeout(...);

Then when you reset your variable to initial status, you can simply:

myTimeOut = null;