React Native TypeScript Transformer
This package, `react-native-typescript-transformer`, provides a mechanism for transpiling TypeScript files within React Native applications. It functions by configuring the React Native packager (Metro) to use the TypeScript compiler directly for transformation, enabling seamless usage of TypeScript with React Native versions starting from 0.45. The current stable version is 1.2.13. However, it is largely considered superseded and deprecated for new projects or existing projects on React Native 0.57 and above, as modern React Native CLI (`react-native init --template typescript`) configures Babel for TypeScript transpilation by default. The key differentiator is its use of the TypeScript compiler for transpilation, unlike Babel which only strips types, requiring a separate `tsc --noEmit` command for type checking. Release cadence is infrequent, reflecting its deprecated status for current development practices.
Common errors
-
Error: Unable to resolve module `react-native-typescript-transformer`
cause The package `react-native-typescript-transformer` is not installed or Metro cannot find it in your `node_modules`.fixRun `yarn add --dev react-native-typescript-transformer typescript` and ensure your `metro.config.js` or `rn-cli.config.js` correctly references the package. -
TypeError: Cannot read property 'babelTransformerPath' of undefined
cause Your `metro.config.js` is malformed, or you are trying to use the newer `transformer.babelTransformerPath` syntax with an older React Native version that expects `getTransformModulePath()`.fixCheck your `metro.config.js` for syntax errors. Verify your React Native version and use the corresponding configuration structure from the package documentation (e.g., for RN < 0.57, use `getTransformModulePath`). -
Type errors not reported in console during `react-native run-android/ios`
cause The build process is using Babel (which only strips types) or the `noEmit: true` option in `tsconfig.json` prevents `tsc` from generating output, but you are not running `tsc` separately for type checking.fixRun `tsc --noEmit --watch` in a separate terminal during development, or integrate `tsc --noEmit` into your build script or CI/CD pipeline to perform type checking.
Warnings
- deprecated This package is largely deprecated. For new React Native projects or existing projects on RN 0.57+, it is recommended to use `react-native init MyProject --template typescript` which uses Babel for TypeScript transpilation, or follow Babel-based migration guides.
- gotcha When using Babel for TypeScript transpilation (the recommended modern approach), Babel does not perform type checking. You must run the TypeScript compiler (`tsc --noEmit`) separately as a linter or for CI checks to catch type errors.
- gotcha Babel transpilation for TypeScript does not support certain advanced language features: namespaces, bracket-style type assertions (e.g., `<Foo>x`) when JSX is enabled, enums spanning multiple declarations (enum merging), and legacy import/export syntax (`import foo = require(...)`).
- gotcha The platform-specific entry files (e.g., `index.ios.js`, `index.android.js`, `index.js`) must remain JavaScript files (`.js`) and cannot be converted to TypeScript (`.ts` or `.tsx`).
- breaking The configuration for this transformer in the React Native packager (Metro) changed significantly across different React Native versions. RN < 0.57 uses `getTransformModulePath()` and `getSourceExts()`, while RN >= 0.57 uses `transformer.babelTransformerPath`.
Install
-
npm install react-native-typescript-transformer -
yarn add react-native-typescript-transformer -
pnpm add react-native-typescript-transformer
Imports
- transformer.babelTransformerPath
import transformer from 'react-native-typescript-transformer'
module.exports = { transformer: { babelTransformerPath: require.resolve('react-native-typescript-transformer') } } - getTransformModulePath
module.exports = { getTransformModulePath() { return require.resolve('react-native-typescript-transformer'); } } - getSourceExts
module.exports = { getSourceExts() { return ['ts', 'tsx']; } }
Quickstart
{
// tsconfig.json
"compilerOptions": {
"target": "es2015",
"jsx": "react",
"noEmit": true,
"moduleResolution": "node",
"importHelpers": true // Optional, for tslib
},
"exclude": [
"node_modules"
]
}
// metro.config.js (for RN >= 0.59)
module.exports = {
transformer: {
babelTransformerPath: require.resolve('react-native-typescript-transformer')
},
resolver: {
sourceExts: ['js', 'jsx', 'ts', 'tsx', 'json', 'node']
}
};
// Add dev dependencies
// yarn add --dev react-native-typescript-transformer typescript @types/react @types/react-native