Migrating Code to the Strict API

+15 Mana ✨

After an upgrade to 0.87 or later, tsc can reject code that type-checked before, such as a ref typed useRef<View>, a parameter typed AlertStatic or a Jest setup that imports InitializeCore. Most fixes use a replacement exported from react-native or from a public entry point such as react-native/setup-env. A deep import with no root equivalent keeps its import behind a // @ts-expect-error comment. Apart from the InitializeCore import, every fix is a type change that leaves the bundle as it was.

Key terms

  • ViewInstance: the ref type for View and Animated.View, an alias of HostInstance.
  • HostInstance: the base type for a built-in component instance, still exported from react-native.
  • react-native/setup-env: a side-effect module that sets up the core JavaScript environment: global timers, the global console and hooks for printing stack traces with source maps.

Refs use Instance types

useRef<View> used to work because View and the other components were typed as classes. Under the Strict API, built-in components are typed as functions, so View names the function and component type names no longer work as ref types. ScrollView, for example, is now the type of a function component with statics, while its ref prop is typed React.Ref<ScrollViewInstance>.

Each built-in component that supports refs now has a dedicated *Instance type, such as TextInputInstance or ScrollViewInstance, and the fix is to type the ref with it:

BeforeAfter
useRef<View>(null)useRef<ViewInstance>(null)
useRef<HostInstance>(null) for a specific componentuseRef<ViewInstance>(null)
Ref<Animated.LegacyRef<View>>Ref<ViewInstance>

React.ComponentRef<typeof View> still type-checks and produces the same type as ViewInstance, so existing code written that way can stay. The *Instance types are shorter aliases and the recommended form. They also cover Animated variants: one ViewInstance ref can go to View or Animated.View, which replaces the removed Animated.LegacyRef type, so ref={ref as React.Ref<Animated.LegacyRef<View>>} becomes ref={ref}. InputAccessoryView, TouchableWithoutFeedback and experimental_LayoutConformance have no ref support and no instance type.

In the SearchPanel example, useRef<ScrollViewInstance>(null) and useRef<TextInputInstance>(null) match the ref prop types of ScrollView and TextInput, and the props come from ViewProps. TextInputInstance is a class type, so queryRef.current offers methods such as isFocused() and clear().

Type names that are gone

The *Static types were removed. They used to be a *Static type plus a variable of that type, and most had an alias with the value's name, but not all. The docs list many affected names, among them AlertStatic, ActionSheetIOSStatic, ToastAndroidStatic, InteractionManagerStatic, UIManagerStatic, PlatformStatic, SectionListStatic, AppStateStatic and KeyboardStatic. Some never had an alias with the value's name, such as InteractionManagerStatic, KeyboardStatic and PixelRatioStatic. Under the Strict API, import type {AlertStatic} from 'react-native' fails with error TS2305 (Module '"react-native"' has no exported member 'AlertStatic'). Alert is now declared as a class with only static methods, so the type of the Alert value is typeof Alert, and function notify(alert: AlertStatic) becomes function notify(alert: typeof Alert). The bare Alert type names the class's empty instance type, which has no alert() or prompt(). Linking is now both the value and the type, so a parameter typed Linking keeps working.

The long-deprecated *Properties aliases are inaccessible too. Each has a direct *Props replacement: ViewProperties becomes ViewProps, TextInputProperties becomes TextInputProps, and ImagePropertiesSourceOptions becomes ImageSourcePropType. Code that already uses ViewProps needs no change.

A few shapes changed as well:

  • Every optional prop is typed as type | undefined.
  • Props components never used are gone, such as lineBreakMode on Text and scrollWithoutAnimationTo on ScrollView.
  • Internal helper types are no longer reachable, such as RecursiveArray, RegisteredStyle and Falsy from StyleSheet, or WithAnimatedArray and WithAnimatedObject from Animated.
  • Animated.Value and the arithmetic nodes, such as AnimatedAddition and AnimatedDiffClamp, are non-generic, so Animated.Value<number> fails with error TS2315 (Type 'AnimatedValue' is not generic). AnimatedInterpolation keeps its output type parameter, and interpolate is generic.

Test setup and mocks

In most projects, test mocks need no change. A jest.mock() call against a react-native/* path keeps working, because the Strict API doesn't change module resolution in Jest or Metro, and the path string isn't type-checked.

Importing a deep path as a module is different. If a test imports one, for example to wrap the real implementation from jest.requireActual(), TypeScript reports the import as untyped. Where no root export covers the case, keep the import and put a // @ts-expect-error comment on the line above it.

A Jest setup that imports react-native/Libraries/Core/InitializeCore must be updated: replace that import with import 'react-native/setup-env', as the Jest setup example shows. InitializeCore is a side-effect entry point with no root equivalent, it is deprecated since 0.87, and it now only requires setup-env. Most apps never import either module, because Metro's React Native defaults run react-native/setup-env before your main module. You will mostly find the old import in Jest setup files and custom entry points.

Code Examples

tsx
import {useRef} from 'react';
import {ScrollView, TextInput, View} from 'react-native';
import type {ScrollViewInstance, TextInputInstance, ViewProps} from 'react-native';

export function SearchPanel(props: ViewProps) {
  const listRef = useRef<ScrollViewInstance>(null);
  const queryRef = useRef<TextInputInstance>(null);

  return (
    <View {...props}>
      <TextInput ref={queryRef} />
      <ScrollView ref={listRef} />
    </View>
  );
}
typescript
// jest.setup.ts
import 'react-native/setup-env';

jest.mock('react-native/Libraries/AppState/NativeAppState');
✓ Completed