Skip to content

Enabling "Strict TypeScript API" doesn't restrict access to react-native/Libraries/* #53565

Description

@kraenhansen

Description

Setting react-native-strict-api doesn't restrict access to sub-paths like react-native/Libraries/Alert/Alert.

Steps to reproduce

In an attempt to verify the restriction of types by setting the react-native-strict-api in the customCondition of the tsconfig.json I followed the guide on the React Native Blog: https://reactnative.dev/blog/2025/06/12/moving-towards-a-stable-javascript-api#breaking-deep-imports-are-disallowed and found that I could still access APIs through deep imports:

src/index.ts

import{Alert}from"react-native/Libraries/Alert/Alert";

package.json

{
"name": "react-native-ts-test",
"private": true,
"version": "0.1.0",
"scripts": {
"build": "tsc --noEmit"
},
"devDependencies": {
"@react-native/typescript-config": "^0.81.1",
"typescript": "5.9.2",
"jest": "^30.1.2"
},
"dependencies": {
"react-native": "^0.81.1"
}
}

tsconfig.json

{
"extends": "@react-native/typescript-config",
"compilerOptions": {
"customConditions": ["react-native-strict-api"]
},
"include": ["src/index.ts"]
}

See the resolution (running npx resolution-explorer)

Explicitly specified module resolution kind: 'Bundler'. Resolving in CJS mode with conditions 'import', 'types', 'react-native-strict-api'. File '/Users/kraen.hansen/Repositories/react-native-ts-test/src/package.json' does not exist. Found 'package.json' at '/Users/kraen.hansen/Repositories/react-native-ts-test/package.json'. Loading module 'react-native/Libraries/Alert/Alert' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON. Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration. Directory '/Users/kraen.hansen/Repositories/react-native-ts-test/src/node_modules' does not exist, skipping all lookups in it. Found 'package.json' at '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native/package.json'. Entering conditional exports. Matched 'exports' condition 'react-native-strict-api'. package.json scope '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native' explicitly maps specifier './Libraries/Alert/Alert' to null. Failed to resolve under condition 'react-native-strict-api'. Saw non-matching condition 'react-native-strict-api-UNSAFE-ALLOW-SUBPATHS'. Matched 'exports' condition 'types'. Using 'exports' subpath './*' with target './Libraries/Alert/Alert.d.ts'. File '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native/Libraries/Alert/Alert.d.ts' exists - use it as a name resolution result. 'package.json' has a 'peerDependencies' field. Resolving real path for '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native', result '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native'. Failed to find peerDependency '@types/react'. Found 'package.json' at '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react/package.json'. Found peerDependency 'react' with '19.1.1' version. Resolved under condition 'types'. Exiting conditional exports. Resolving real path for '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native/Libraries/Alert/Alert.d.ts', result '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native/Libraries/Alert/Alert.d.ts'. ======== Module name 'react-native/Libraries/Alert/Alert' was successfully resolved to '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native/Libraries/Alert/Alert.d.ts' with Package ID 'react-native/Libraries/Alert/Alert.d.ts@0.81.1+react@19.1.1'. ======== 

Crucially, notice:

  • package.json scope '/Users/kraen.hansen/Repositories/react-native-ts-test/node_modules/react-native' explicitly maps specifier './Libraries/Alert/Alert' to null. 👍
  • Matched 'exports' condition 'types'. 😱
  • Using 'exports' subpath './*' with target './Libraries/Alert/Alert.d.ts'. 😱

I verified this with multiple TypeScript versions: 5.0.2, 5.7.3, 5.9.2.

React Native Version

0.81.1

Affected Platforms

Build - MacOS, Build - Windows, Build - Linux

Output of npx @react-native-community/cli info

N/A

Stacktrace or Logs

N/A

MANDATORY Reproducer

https://github.com/react-native-community/reproducer-react-native

Screenshots and Videos

No response

Metadata

Metadata

Assignees

Labels

Needs: AttentionIssues where the author has responded to feedback.Needs: ReproThis issue could be improved with a clear list of steps to reproduce the issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions