Member-only story

Resolving React Native Typescript error: “Type ‘Timeout’ is not assignable to type ‘number’”

Davyd NRB
Dec 27, 2022

--

This typescript issue is easily fixable in your React Native project (for example after updating RN to the latest version)

However, the lib definition provides the proper tygins for global functions:

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

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

The main problem is that global types @type/node replace global types @type/react-native.

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"]
}
}

After modifying compilerOptions.types nodejs typing will be exclude.

Read more: https://www.typescriptlang.org/tsconfig#types

--

--

Davyd NRB
Davyd NRB

Written by Davyd NRB

React Native guru 🧘\ DevOps adjutant \ Linux lover ❤️

No responses yet

Write a response