DEV Community

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

Posted on

2 2

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)

typescript

11 Tips That Make You a Better Typescript Programmer

1 Think in {Set}

Type is an everyday concept to programmers, but it’s surprisingly difficult to define it succinctly. I find it helpful to use Set as a conceptual model instead.

#2 Understand declared type and narrowed type

One extremely powerful typescript feature is automatic type narrowing based on control flow. This means a variable has two types associated with it at any specific point of code location: a declaration type and a narrowed type.

#3 Use discriminated union instead of optional fields

...

Read the whole post now!

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay