Skip to content

Commit 9bb4c34

Browse files
committed
[docs] Update Strict TypeScript API guide
- Simplification pass. - Add/consolidate ref type guidance.
1 parent bf18e60 commit 9bb4c34

1 file changed

Lines changed: 146 additions & 43 deletions

File tree

docs/strict-typescript-api.md

Lines changed: 146 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,21 @@ id: strict-typescript-api
33
title: Strict TypeScript API (opt in)
44
---
55

6+
import Tabs from '@theme/Tabs';
7+
import TabItem from '@theme/TabItem';
68
import RNRepoLink from '@site/core/RNRepoLink';
79

8-
The Strict TypeScript API is a preview of our future, stable JavaScript API for React Native.
10+
<p><div className="label primary">Since 0.80</div></p>
911

10-
Specifically, this is a new set of TypeScript types for the `react-native` npm package, available from 0.80 onwards. These provide stronger and more futureproof type accuracy, and will allow us to confidently evolve React Native's API into a stable shape. Opting in to the Strict TypeScript API brings some structural type differences, and is therefore a one-time breaking change.
12+
The Strict TypeScript API is a new set of TypeScript types for the `react-native` package, providing a refined single package entry point and stronger type accuracy.
1113

12-
The new types are:
13-
14-
1. **Generated directly from our source code** — improving coverage and correctness, so you can expect stronger compatibility guarantees.
15-
2. **Restricted to `react-native`'s index file** — more tightly defining our public API, and meaning we won't break the API when making internal file changes.
16-
17-
When the community is ready, the Strict TypeScript API will become our default API in future — synchronized with deep imports removal.
14+
The Strict API is currently in preview as we iterate towards a stable JavaScript API for React Native.
1815

1916
## Opting in
2017

21-
We're shipping these new types alongside our existing types, meaning you can choose to migrate when ready. We encourage early adopters and newly created apps to opt in via your `tsconfig.json` file.
18+
We're shipping these new types alongside our existing types, meaning you can choose to migrate when ready, via your `tsconfig.json` config.
2219

23-
Opting in is a **breaking change**, since some of our new types have updated names and shapes, although many apps won't be affected. You can learn about each breaking change in the next section.
20+
Opting in brings some structural type differences, including updated type names and shapes. Therefore migrating your codebase to the Strict API is a **one-time breaking change**.
2421

2522
```json title="tsconfig.json"
2623
{
@@ -38,25 +35,31 @@ This will instruct TypeScript to resolve `react-native` types from our new [`typ
3835

3936
:::
4037

41-
The Strict TypeScript API follows our [RFC](https://github.com/react-native-community/discussions-and-proposals/pull/894) to remove deep imports from React Native. Therefore, some APIs are no longer exported at root. This is intentional, in order to reduce the overall surface area of React Native's API.
38+
### Key changes (breaking)
4239

43-
:::tip[API feedback]
40+
1. **No deep imports.** The API is restricted to `react-native`'s index file. This is a tighter and more intentional public API contract. It also ensures that internal file path changes in React Native's source code won't be breaking.
41+
2. **Generated directly from source.** Previously, React Native used separately maintained manual types. Generating from source now means we improve coverage, correctness, and compatibility guarantees.
4442

45-
**Sending feedback**: We will be working with the community to finalize which APIs we export over (at least) the next two React Native releases. Please share your feedback in our [feedback thread](https://github.com/react-native-community/discussions-and-proposals/discussions/893).
43+
---
4644

47-
See also our [announcement blog post](/blog/2025/06/12/moving-towards-a-stable-javascript-api) for more info on our motivation and timelines.
45+
:::tip[Preview feedback]
46+
47+
We're working with the community and partners to finalize the shape of the Strict API. Share API feedback in our [discussion thread](https://github.com/react-native-community/discussions-and-proposals/discussions/893), or see the [announcement blog post](/blog/2025/06/12/moving-towards-a-stable-javascript-api) for more context.
4848

4949
:::
5050

5151
## Migration guide
5252

53-
### Codegen types should now be imported from the `react-native` package
53+
### Codegen types `CodegenTypes` namespace
5454

5555
Types used for codegen, like `Int32`, `Double`, `WithDefault` etc. are now available under a single `CodegenTypes` namespace. Similarly, `codegenNativeComponent` and `codegenNativeCommands` are now available to import from the react-native package instead of using the deep import.
5656

5757
Namespaced `CodegenTypes` as well as `codegenNativeCommands` and `codegenNativeComponent` are also available from `react-native` package when the Strict API is not enabled to make the adoption easier for third-party libraries.
5858

59-
**Before**
59+
#### Migration
60+
61+
<Tabs defaultValue="after">
62+
<TabItem value="before" label="Before">
6063

6164
```ts title=""
6265
import codegenNativeComponent from 'react-native/Libraries/Utilities/codegenNativeComponent';
@@ -75,7 +78,8 @@ export default codegenNativeComponent<NativeProps>(
7578
);
7679
```
7780

78-
**After**
81+
</TabItem>
82+
<TabItem value="after" label="After">
7983

8084
```ts title=""
8185
import {CodegenTypes, codegenNativeComponent} from 'react-native';
@@ -90,9 +94,124 @@ export default codegenNativeComponent<NativeProps>(
9094
);
9195
```
9296

97+
</TabItem>
98+
</Tabs>
99+
100+
### Refs now use `*Instance` types
101+
102+
Each built-in component now has a dedicated `*Instance` type for use with refs — for example, `ViewInstance`, `TextInputInstance`, `ScrollViewInstance`. These are the **recommended way to type refs** under the Strict TypeScript API.
103+
104+
Previously, `useRef<View>` worked because `View` and other components were typed as a class. Under the Strict API, built-in components are typed as functions, so `View` refers to the function itself — **component type names no longer work as ref types**.
105+
106+
<Tabs defaultValue="after">
107+
<TabItem value="before" label="Before">
108+
109+
```tsx title=""
110+
import {useRef} from 'react';
111+
import {View, TextInput} from 'react-native';
112+
113+
function MyComponent() {
114+
const viewRef = useRef<View>(null);
115+
const inputRef = useRef<TextInput>(null);
116+
117+
return (
118+
<>
119+
<View ref={viewRef} />
120+
<TextInput ref={inputRef} />
121+
</>
122+
);
123+
}
124+
```
125+
126+
</TabItem>
127+
<TabItem value="after" label="After">
128+
129+
```tsx title=""
130+
import {useRef} from 'react';
131+
import type {ViewInstance, TextInputInstance} from 'react-native';
132+
133+
function MyComponent() {
134+
const viewRef = useRef<ViewInstance>(null);
135+
const inputRef = useRef<TextInputInstance>(null);
136+
137+
return (
138+
<>
139+
<View ref={viewRef} />
140+
<TextInput ref={inputRef} />
141+
</>
142+
);
143+
}
144+
```
145+
146+
</TabItem>
147+
</Tabs>
148+
149+
`*Instance` types also work transparently with `Animated` variants — no separate type is needed:
150+
151+
```tsx title=""
152+
const viewRef = useRef<ViewInstance>(null);
153+
154+
<View ref={viewRef} />
155+
<Animated.View ref={viewRef} />
156+
```
157+
158+
This also replaces the removed `Animated.LegacyRef` type. Code using `ref={ref as React.Ref<Animated.LegacyRef<View>>}` can be simplified to `ref={ref}` with a `ViewInstance`-typed ref.
159+
160+
<details>
161+
<summary>**🗒️ Available instance types**</summary>
162+
163+
| Component | Instance type |
164+
| ------------------------- | --------------------------------- |
165+
| `ActivityIndicator` | `ActivityIndicatorInstance` |
166+
| `Button` | `ButtonInstance` |
167+
| `DrawerLayoutAndroid` | `DrawerLayoutAndroidInstance` |
168+
| `FlatList` | `FlatListInstance` |
169+
| `Image` | `ImageInstance` |
170+
| `ImageBackground` | `ImageBackgroundInstance` |
171+
| `KeyboardAvoidingView` | `KeyboardAvoidingViewInstance` |
172+
| `Modal` | `ModalInstance` |
173+
| `Pressable` | `PressableInstance` |
174+
| `ProgressBarAndroid` | `ProgressBarAndroidInstance` |
175+
| `RefreshControl` | `RefreshControlInstance` |
176+
| `SafeAreaView` | `SafeAreaViewInstance` |
177+
| `ScrollView` | `ScrollViewInstance` |
178+
| `SectionList` | `SectionListInstance` |
179+
| `StatusBar` | `StatusBarInstance` |
180+
| `Switch` | `SwitchInstance` |
181+
| `Text` | `TextInstance` |
182+
| `TextInput` | `TextInputInstance` |
183+
| `TouchableHighlight` | `TouchableHighlightInstance` |
184+
| `TouchableNativeFeedback` | `TouchableNativeFeedbackInstance` |
185+
| `TouchableOpacity` | `TouchableOpacityInstance` |
186+
| `View` | `ViewInstance` |
187+
| `VirtualizedList` | `VirtualizedListInstance` |
188+
| `VirtualizedSectionList` | `VirtualizedSectionListInstance` |
189+
190+
Components without ref support (`InputAccessoryView`, `TouchableWithoutFeedback`, `experimental_LayoutConformance`) do not have instance types.
191+
192+
</details>
193+
194+
**Migration**
195+
196+
| Before | After |
197+
| ------------------------------------------------------- | ---------------------------- |
198+
| `useRef<View>(null)` | `useRef<ViewInstance>(null)` |
199+
| `useRef<React.ComponentRef<typeof View>>(null)` | `useRef<ViewInstance>(null)` |
200+
| `useRef<HostInstance>(null)` (for a specific component) | `useRef<ViewInstance>(null)` |
201+
| `Ref<Animated.LegacyRef<View>>` | `Ref<ViewInstance>` |
202+
203+
:::note
204+
205+
`React.ComponentRef<typeof View>` remains valid and produces the same type as `ViewInstance`. The `*Instance` types are convenient aliases — both approaches work.
206+
207+
:::
208+
93209
### Removal of `*Static` types
94210

95-
**Before**
211+
#### Migration
212+
213+
<Tabs defaultValue="after">
214+
<TabItem value="before" label="Before">
96215

97216
```tsx title=""
98217
import {Linking, LinkingStatic} from 'react-native';
@@ -101,7 +220,8 @@ function foo(linking: LinkingStatic) {}
101220
foo(Linking);
102221
```
103222

104-
**After**
223+
</TabItem>
224+
<TabItem value="after" label="After">
105225

106226
```tsx title=""
107227
import {Linking} from 'react-native';
@@ -110,11 +230,13 @@ function foo(linking: Linking) {}
110230
foo(Linking);
111231
```
112232

113-
The following APIs were previously named as `*Static` plus a variable declaration of said type. In most cases there was an alias so that value and the type were exported under the same identifier, but some were missing.
233+
</TabItem>
234+
</Tabs>
114235

115-
(For example there was an `AlertStatic` type, `Alert` variable of type `AlertStatic` and type `Alert` which was an alias for `AlertStatic`. But in the case of `PixelRatio` there was a `PixelRatioStatic` type and a `PixelRatio` variable of that type without additional type aliases.)
236+
The following APIs were previously named as `*Static` plus a variable declaration of said type. In most cases there was an alias so that value and the type were exported under the same identifier, but some were missing.
116237

117-
**Affected APIs**
238+
<details>
239+
<summary>**🗒️ Affected APIs**</summary>
118240

119241
- `AlertStatic`
120242
- `ActionSheetIOSStatic`
@@ -145,34 +267,15 @@ The following APIs were previously named as `*Static` plus a variable declaratio
145267
- `SettingsStatic`
146268
- `VibrationStatic`
147269

148-
### Some core components are now function components instead of class components
149-
150-
- `View`
151-
- `Image`
152-
- `TextInput`
153-
- `Modal`
154-
- `Text`
155-
- `TouchableWithoutFeedback`
156-
- `Switch`
157-
- `ActivityIndicator`
158-
- `ProgressBarAndroid`
159-
- `InputAccessoryView`
160-
- `Button`
161-
- `SafeAreaView`
162-
163-
Due to this change, accessing ref types of these views requires using `React.ComponentRef<typeof View>` pattern which works as expected for both class and function components, e.g.:
164-
165-
```ts title=""
166-
const ref = useRef<React.ComponentRef<typeof View>>(null);
167-
```
270+
</details>
168271

169272
## Other breaking changes
170273

171274
### Changes to Animated types
172275

173276
Animated nodes were previously generic types based on their interpolation output. Now, they are non-generic types with a generic `interpolate` method.
174277

175-
`Animated.LegacyRef` is no longer available.
278+
`Animated.LegacyRef` is no longer available. Use the appropriate `*Instance` type instead (e.g. `ViewInstance` for `Animated.View`).
176279

177280
### Unified types for optional props
178281

0 commit comments

Comments
 (0)