Skip to content

Commit eb368f9

Browse files
committed
Add detail on the Strict TypeScript API
1 parent 49ce56d commit eb368f9

File tree

1 file changed

+70
-1
lines changed

1 file changed

+70
-1
lines changed

proposals/0894-deprecate-subpath-imports.md

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ import {Alert, AlertType} from 'react-native';
8484

8585
By removing subpath imports, we will scope the `react-native` package down to a narrower, enumerable public API, using the existing `index.js` and `index.d.ts` exports as a starting point.
8686

87-
All existing APIs exported from the main `'react-native'` import path will be **unchanged**. As of March 2025, we now define a strict list of type exports, see [`index.js.flow`](/TODO) — symbols that are not in this file will not be part of the public API.
87+
All existing APIs exported from the main `'react-native'` import path will be **unchanged**. As of March 2025, we now define a strict list of type exports, see [`index.js.flow`](https://github.com/facebook/react-native/blob/main/packages/react-native/index.js.flow) — symbols that are not in this file will not be part of the public API.
8888

8989
#### Framework considerations
9090

@@ -141,6 +141,10 @@ import {Alert} from 'react-native/Libraries/Alert/Alert';
141141

142142
**Under the hood**: As we are unsure about the % usage of [@react-native/eslint-config](https://www.npmjs.com/package/@react-native/eslint-config), we believe integrating via Metro is important for consistent visibility.
143143

144+
#### Removing subpaths from our TypeScript API (opt-in)
145+
146+
See [Parallel effort: Strict TypeScript API](#parallel-effort-strict-typescript-api).
147+
144148
### Eventual strong enforcement
145149

146150
Again, it's important that we give the community sufficient time to adapt to this change before we action a hard removal — we anticipate this will be during **H2 2025**.
@@ -159,3 +163,68 @@ This will functionally disallow unlisted subpath imports under TypeScript, Metro
159163
}
160164
}
161165
```
166+
167+
---
168+
169+
## Parallel effort: Strict TypeScript API
170+
171+
> 🟢 In active development as of January 2025.
172+
173+
#### Discouraging use by removing subpaths under TypeScript
174+
175+
A parallel part of our adoption strategy is a new version of React Native's TypeScript API, auto-translated from the source codebase in Flow. While a separate effort, this forms an incentivising mechanism for removing subpath imports, on top of console and ESLint warnings.
176+
177+
This new API will no longer expose internal `react-native` modules under TypeScript — resulting in type errors against existing uses, and preventing new deep imports which may have occurred from auto-importing in the past.
178+
179+
#### User opt-in
180+
181+
By default, TypeScript will continue to use the existing manually defined types included in the `react-native` package, which allow subpaths.
182+
183+
When we ship the new Strict TypeScript API, it will be available as a user opt-in — by specifying the `"react-native-strict-api"` condition in a project's `tsconfig.json`.
184+
185+
```json
186+
{
187+
"compilerOptions": {
188+
"customConditions": ["react-native-strict-api"]
189+
}
190+
}
191+
```
192+
193+
<small>See [TSConfig Reference - `customConditions`](https://www.typescriptlang.org/tsconfig/#customConditions).</small>
194+
195+
Under the hood, this opt-in will be implemented in React Native via a lenient `package.json` `"exports"` mapping, exposing only `"." → "types_generated/index.d.ts"` under this condition.
196+
197+
```json5
198+
{
199+
"exports": {
200+
".": {
201+
"react-native-strict-api": "./types_generated/index.d.ts",
202+
"default": "./index.js"
203+
},
204+
"./*": {
205+
"react-native-strict-api": null, // ← 🚫 Subpath imports disallowed
206+
"default": "./*.js"
207+
}
208+
}
209+
}
210+
```
211+
212+
213+
- The scope of this opt-in will be for the immediately analysed TypeScript project root, and will have no impact at runtime or on dependent projects.
214+
- Having an user opt-in will be necessary both for subpath import removal, as well as breaking changes to some type shapes now generated from source (increasing correctness, consistency, and ergonomics).
215+
- As with subpath import deprecation, we may consider an escape hatch for Frameworks, based on feedback.
216+
217+
#### Final state (coordinated removal of subpath imports)
218+
219+
The final state (potentially with a preceding opt-out state) will be a hard rollout of the new types in a future version of React Native, aligned with when we remove subpath imports in our `"exports"` mapping.
220+
221+
```json5
222+
{
223+
"exports": {
224+
".": {
225+
"types": "./types_generated/index.d.ts",
226+
"default": "./index.js"
227+
}
228+
}
229+
}
230+
```

0 commit comments

Comments
 (0)