Typescript: Type 'number' is not assignable to type 'Timeout'

Created on 27 Feb 2019  路  4Comments  路  Source: microsoft/TypeScript


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

Working as Intended

Most helpful comment

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.

All 4 comments

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)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DanielRosenwasser picture DanielRosenwasser  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

CyrusNajmabadi picture CyrusNajmabadi  路  3Comments

blendsdk picture blendsdk  路  3Comments

seanzer picture seanzer  路  3Comments