Skip to content

Instantly share code, notes, and snippets.

@yooouuri
Created December 3, 2018 11:42
Show Gist options
  • Save yooouuri/2f8a3bd635cee10620358d080f023397 to your computer and use it in GitHub Desktop.
Save yooouuri/2f8a3bd635cee10620358d080f023397 to your computer and use it in GitHub Desktop.
add Typescript to React Native

Add to devDependencies in package.json:

  • @types/react
  • @types/react-native
  • react-native-typescript-transformer
  • typescript
yarn add --dev typescript react-native-typescript-transformer @types/react @types/react-native

Create rn-cli.config.js and tsconfig.json in the root of your project.

rn-cli.config.js

module.exports = {
  getTransformModulePath() {
    return require.resolve('react-native-typescript-transformer');
  },
  getSourceExts() {
    return ['ts', 'tsx'];
  }
};

tsconfig.json:

{
    "compilerOptions": {
        "target": "es2015",
        "jsx": "react",
        "noEmit": true,
        "moduleResolution": "node",
        "allowSyntheticDefaultImports": true
    },
    "exclude": [
        "node_modules"
    ]
}

Change App.js to App.tsx

App.tsx

- type Props = {}
+ interface Props { }
+ interface State { }

- export default class App extends React<Props>
+ export default class App extends React<Props, State> {
  // ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment