Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have two TypeScript packages, and one package (Package A) depends on the other (Package B). Each package has a unit test set up using Karma. When I run unit tests for each individually after installing all dependencies from NPM, the unit tests run fine. However, if I use npm link package-b in Package A and run Package A's unit tests then, I get the error stated in the title: "TS2322: Type 'Timeout' is not assignable to type 'number'."

The line in question is a call to setTimeout. After digging, I found that while running the tests separately without npm link, TypeScript correctly identifies the setTimeout signature in typescript/lib/lib.dom as the desired type, but in the failing case after using npm link it is using using Node's setTimeout signature in @types/node/index. I confirmed this by changing the return type on setTimeout to string and observing the same error with string in the place of Timeout.

What I am not certain of is why the TypeScript compiler has decided to use the alternative definition in this specific case, nor how I can convince it to use the desired definition. I am happy to post some code, but I am not sure what would be useful in this case given all that is on the failing line is the setTimeout call.

like image 838
dawsonc623 Avatar asked Apr 06 '19 14:04

dawsonc623


1 Answers

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

like image 160
Pablo Marcano Avatar answered Sep 22 '22 06:09

Pablo Marcano