patterntypescriptCritical
"TS2322: Type 'Timeout' is not assignable to type 'number'" when running unit tests
Viewed 0 times
ts2322assignablerunningunittimeoutnotnumberwhenteststype
Problem
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
The line in question is a call to
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
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.Solution
You could try with using
window.setTimeout instead of just setTimeout, this way the typescript one will be explicitly usedContext
Stack Overflow Q#55550096, score: 328
Revisions (0)
No revisions yet.