This post uses Microsoft's TypeScript-React-Native-Starter repo as a guide.
Update: Since this blog post was written, Babel 7 was released with integrated TypeScript support. Babel 7 replaces all the set up described in this blog post with just one command:
However, there are some limitations to Babel's TypeScript support, which the blog post above goes into in detail. The steps outlined in this post still work, and Artsy is still using react-native-typescript-transformer in production, but the fastest way to get up and running with React Native and TypeScript is using the above command. You can always switch later if you have to.
In any case, have fun! The original blog post continues below.
Because you might be developing on one of several different platforms, targeting several different types of devices, basic setup can be involved. You should first ensure that you can run a plain React Native app without TypeScript. Follow the instructions on the React Native website to get started. When you've managed to deploy to a device or emulator, you'll be ready to start a TypeScript React Native app.
Once you've tried scaffolding out an ordinary React Native project, you'll be ready to start adding TypeScript. Let's go ahead and do that.
The next step is to add TypeScript to your project. The following commands will:
- add TypeScript to your project
- add React Native TypeScript Transformer to your project
- initialize an empty TypeScript config file, which we'll configure next
- add an empty React Native TypeScript Transformer config file, which we'll configure next
- adds typings for React and React Native
Okay, let's go ahead and run these.
tsconfig.json file contains all the settings for the TypeScript compiler. The defaults created by the command above are mostly fine, but open the file and uncomment the following line:
rn-cli.config.js contains the settings for the React Native TypeScript Transformer. Open it and add the following:
Rename the generated
__tests_/App.js files to
index.js needs to use the
.js extension. All new files should use the
.tsx extension (or
.ts if the file doesn't contain any JSX).
If you tried to run the app now, you'd get an error like
object prototype may only be an object or null. This is caused by a failure to import the default export from React as well as a named export on the same line. Open
App.tsx and modify the import at the top of the file:
Some of this has to do with differences in how Babel and TypeScript interoperate with CommonJS modules. In the future, the two will stabilize on the same behaviour.
At this point, you should be able to run the React Native app.
Then, we'll open up our
package.json and replace the
jest field with the following:
This will configure Jest to run
.tsx files with
To get the best experience in TypeScript, we want the type-checker to understand the shape and API of our dependencies. Some libraries will publish their packages with
@types/ npm scope.
For example, here we'll need types for Jest, React, and React Native, and React Test Renderer.
We saved these declaration file packages to our dev dependencies because this is a React Native app that only uses these dependencies during development and not during runtime. If we were publishing a library to NPM, we might have to add some of these type dependencies as regular dependencies.
You can read more here about getting
For your source control, you'll want to start ignoring the
.jest folder. If you're using git, we can just add entries to our
As a checkpoint, consider committing your files into version control.
Let's add a component to our app. Let's go ahead and create a
Hello.tsx component. It's a pedagogical component, not something that you'd actually write in an app, but something nontrivial that shows off how to use TypeScript in React Native.
components directory and add the following example.
Whoa! That's a lot, but let's break it down:
- Instead of rendering HTML elements like
h1, etc., we're rendering components like
Button. These are native components that work across different platforms.
- Styling is specified using the
StyleSheet.createfunction that React Native gives us. React's stylesheets allow us to control our layout using Flexbox, and style using other constructs similar to those in CSS.
Now that we've got a component, let's try testing it.
We already have Jest installed as a test runner. We're going to write snapshot tests for our components, let's add the required add-on for snapshot tests:
Now let's create a
__tests__ folder in the
components directory and add a test for
The first time the test is run, it will create a snapshot of the rendered component and store it in the
components/__tests__/__snapshots__/Hello.tsx.snap file. When you modify your component, you'll need to update the snapshots and review the update for inadvertent changes. You can read more about testing React Native components here.
Check out the official React tutorial and state-management library Redux. These resources can be helpful when writing React Native apps. Additionally, you may want to look at ReactXP, a component library written entirely in TypeScript that supports both React on the web as well as React Native.
Have fun in a more type-safe React Native development environment!