TypeScript Version: 3.3.3
Search Terms:
Code
var timerId: NodeJS.Timeout = setTimeout(() => {}, 1000)
My config:
{
"compilerOptions": {
"alwaysStrict": true,
"esModuleInterop": true,
"jsx": "react",
"module": "commonjs",
"noImplicitReturns": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"outDir": "build",
"resolveJsonModule": true,
"rootDirs": [
"src",
"assets"
],
"skipLibCheck": true,
"strict": true,
"target": "es2017",
"lib": ["es2017", "dom"],
"typeRoots": [
"./types",
"node_modules/@types"
]
},
"exclude": [
"node_modules"
],
"include": [
"src/**/*.ts",
"src/**/*.tsx"
]
}
Expected behavior:
No error. I have @types/node installed.
Actual behavior:
Error.
Playground Link:
Related Issues:
https://github.com/Microsoft/TypeScript/issues/842
You included the DOM typings in your lib, so TypeScript thinks you're calling the DOM version of setTimeout because it's basically ambiguous given two definitions of it. You can write global.setTimeout to disambiguiate.
Thanks @RyanCavanaugh. I guess I'll move on with global.. Thanks!
For anyone else having this error what worked for me is adding in the tsconfig.js file:
"compilerOptions": {
...
"types": [],
}
The compiler is getting confused between global.setTimeout() and window.setTimeout(). In our application, we intended to use the browser's setTimeout method, so this resolved the mismatched types:
var timerId: number = window.setTimeout(() => {}, 1000)
Most helpful comment
You included the DOM typings in your
lib, so TypeScript thinks you're calling the DOM version ofsetTimeoutbecause it's basically ambiguous given two definitions of it. You can writeglobal.setTimeoutto disambiguiate.