Using TypeScript with React Native
In this tutorial, you are going to learn how to set up a React Native project with TypeScript.
Note: This tutorial assumes that you are familiar with TypeScript and React. Otherwise, you might first want to read up on them.
An older blog post on the React Native site describes how to set up React Native with TypeScript. It's a little outdated, though.
Things have changed since then. Since version 0.57 the framework ships with TypeScript support. And there is an excellent template for RN with TypeScript. This post is an update and an enhancement for the instructions of that old article. In addition to Jest, we will also go into detail on how to set up linting, Enzyme and React Native Testing Library.
Let's get started.
1. Creating the Project
Using the React Native CLI and Emin's template we can generate the project with TypeScript pre-installed.
It's as easy as that ππ».
2. Setting Up TSLint
Next, we want to set up linting. I like a combination of three rules:
You can check out what the respective configuration does by clicking on it. Let's install these rules together with TSLint now.
Consequently, we want to set up a tslint.json
file. In it, you can fine-tune your linting configuration (below you'll see my personal preference).
You might also want to change the lib
key in your tsconfig.json
from ["es6"]
to ["es2017"]
to have access to newer syntax such as Object.value
.
If your editor supports TSLint integration, it should already complain about the empty Props
interface in App.tsx
.ΒΉ If your editor does not, add the following value to your scripts
key in your package.json
:
Now you can lint using your terminal.
If you set up everything correctly, this will yell at you about the empty interface in App.js
.
3. Setting Up Jest
Jest is already installed by default π. We do need to install ts-jest, though.
And we need to configure it for TypeScript.
Create a file called tsconfig.jest.json
. It is going to tell our ts-jest
preprocessor to translate our code properly into common JavaScript.
Afterwards, add the following to the jest
key in your package.json
so that Jest looks for TypeScript files and knows how to transform them.
Create a file called App.test.tsx
. In it, we will create a basic test to see if Jest works.
Run npm test
to see if it works.
Ta-da π. It works.
Note: Most people either use Enzyme or React Native Testing Library. So you probably only want to follow one of 4. a) and 4. b).
4. a) Setting Up Enzyme
Enzyme requires 5 packages, two of which are types. Let's install them.
Moreover, we need to configure Enzyme. Create a folder in your projectβs root directory called tests/
and create a file called setup.js
in it.
This file tells Enzyme to use the adapter for React 16.
Jest needs to know about this setup file. Modify the jest
key in your package.json
to include the following:
We can test our installation by writing a simple test with Enzyme in App.test.tsx
.
Run the test: npm test
.
Success, Enzyme runs πΊπ».
4. b) Setting Up React Native Testing Library
We only need one package for React Native Testing Library.
And again a simple test to see if it works.
Run npm test
.
Well done π! Your React Native project is configured with TypeScript.
Next Steps
If you are new to React Native check out the beginner tutorial. Otherwise, happy coding! βοΈ
Footnotes
- If your editor supports TSLint integration but does not yell at you at this point, you might need to restart your editor for the changes to take effect.