Chapter 1 · Practice 4 of 5
Import only from the react-native root with the strict TypeScript API
Why it matters
In React Native 0.87 the Strict TypeScript API is the default. Types are generated from React Native's source instead of being written by hand, and they cover only what react-native exports from its root. Deep imports such as react-native/Libraries/... are now type errors.
Refs also get dedicated types such as ViewInstance and TextInputInstance, so you can type a ref without reaching into internals.
If a large codebase cannot migrate at once, you can add react-native-legacy-deep-imports to customConditions in tsconfig.json. That opt-out is available through 0.88 and the legacy types are expected to go after that, so treat it as a deadline, not a setting.
Do
- Replace every
react-native/Libraries/*import with a root import or a library - Type refs with the new instance types such as
TextInputInstance
Avoid
- Leaving the legacy deep imports condition on with no migration plan
Example
// Before: deep import, now a type error
// import TextInputState from 'react-native/Libraries/Components/TextInput/TextInputState';
// After: root import and a typed ref
import { useRef } from 'react';
import { TextInput, type TextInputInstance } from 'react-native';
export function SearchBox() {
const input = useRef<TextInputInstance>(null);
return <TextInput ref={input} placeholder="Search" />;
}