DEV Community

Cover image for React Native fixing typescript issue: "Type 'Timeout' is not assignable to type 'number'"
Davyd NRB
Davyd NRB

Posted on

React Native fixing typescript issue: "Type 'Timeout' is not assignable to type 'number'"

You may encounter this typescript issue in your React Native project with ease (for example after updating RN to the latest version)

The correct tygins for global functions are provided by the lib definition, though:

declare function setTimeout(handler: () => void, timeout: number): number;
declare function setTimeout<Args extends any[]>(
    handler: (...args: Args) => void,
    timeout?: number,
    ...args: Args
): number;
Enter fullscreen mode Exit fullscreen mode

Source: types/react-native/globals.d.ts

The primary issue is that @type/node global types replace @type/react-native global types.

And by default, typescript behaves in that way:

All visible ”@types” packages are included in your compilation.


Ok, then let's specify only that global typing that we actually need (tsconfig.json):

{
  "compilerOptions": {
    "types": ["react-native", "jest"]
  }
}
Enter fullscreen mode Exit fullscreen mode

After modifying compilerOptions.types nodejs typing will be exclude.

Read more:

Top comments (0)