iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🍣

Setting up React Native with TypeScript: A Step-by-Step Guide

に公開

Introduction

This article summarizes how to set up ReactNative + TypeScript.
While I have several ideas for smartphone app development using ReactNative and various libraries, I created this because those articles wouldn't be useful if the setup process wasn't documented.

While the content is essentially an enhanced version of the official tutorial, I have also included solutions for errors I encountered along the way.

Prerequisites

The following environment is assumed:

  • MacOS Catalina@10.15.6
  • XCode@12.0.1
  • VSCode@1.50.1
  • node@10.18.1
  • react@16.13.1
  • react-native@0.63.3

I am using yarn as the package manager, but you can replace it with npm if you prefer.

Creating the Project

Create the project using the following command.
The project name will be ReactNativeTS.

npx react-native init ReactNativeTS --template react-native-template-typescript

Once created, move to the project root directory (hereafter referred to as root).
Since we will be modifying the source code, launch VSCode at the same time.

cd ReactNativeTS
code ./

Installing Related Libraries

Install the related libraries with the following command.
These are the type definition information required for developing with TypeScript.

yarn add --dev typescript @types/jest @types/react @types/react-native @types/react-test-renderer

Creating the src Directory

By default, the source files are located in the root. We will create a src directory and move the files there.
At this point, we will convert *.js files to *.tsx files.
Only App.js is moved by default.

mkdir src
mv App.js src/App.tsx

Modifying index.js Reference

Modify the root index.js as it refers to the App.tsx we just moved.

index.js
- import App from './App';
+ import App from './src/App';

Creating tsconfig.json

Create tsconfig.json since we are writing in TypeScript.

touch tsconfig.json

The content is as follows. If you usually use TypeScript, feel free to add your preferred settings in addition to the following.

tsconfig.json
{
    "compilerOptions": {
        "allowJs": true,
        "allowSyntheticDefaultImports": true,
        "esModuleInterop": true,
        "isolatedModules": true,
        "jsx": "react",
        "lib": ["es6"],
        "moduleResolution": "node",
        "noEmit": true,
        "strict": true,
        "target": "ESNext"
    },
    "exclude": [
        "node_modules",
        "babel.config.js",
        "metro.config.js",    
        "jest.config.js"
    ]
}

Creating jest.config.js

touch jest.config.js
jest.config.js
module.exports = {
    preset: 'react-native',
    moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node']
};

Compilation

Run the compilation using the following command.

yarn tsc

If your versions match the prerequisites, you will likely encounter the following errors.

src/App.tsx:27:18 - error TS2304: Cannot find name 'React$Node'.
27 const App: () => React$Node = () => {
                    ~~~~~~~~~~

src/App.tsx:36:19 - error TS2339: Property 'HermesInternal' does not exist on type 'Global & typeof globalThis'.
36           {global.HermesInternal == null ? null : (
                     ~~~~~~~~~~~~~~

Both errors are occurring in App.tsx, so let's address them.
After resolving each error, run yarn tsc again.

Correcting React$Node

By default, the React$Node module is used, but the correct one is React.ReactNode.

- const App: () => React$Node = () => {
+ const App: () => React.ReactNode = () => {

Resolving HermesInternal

Since global.HermesInternal cannot be resolved, add the following definition.

Add this before const App ....

+ declare const global: {HermesInternal: null | {}};

Execution

Run with the following command.
If it works correctly, the Simulator will start and the following screen will be displayed.

default_screen

Error Tips

In my local environment, there were cases where the app did not start correctly during the execution stage.
Since it seems to be environment-dependent, I will leave the solutions here as Tips.
If you encounter any other errors, I would appreciate it if you could mention them in the comments.

"No bundle URL present" Error

If the above error occurs, you need to generate a main.jsbundle file.
Add the following script to scripts in package.json.

+ "build:ios": "react-native bundle --entry-file='index.js' --bundle-output='./ios/main.jsbundle' --dev=false --platform='ios'"

Run the command you just added.

yarn build:ios

This will generate ios/main.jsbundle.
Next, open ios/ReactNativeTS.xcworkspace from XCode.

Select main.jsbundle under the ReactNativeTS/ReactNativeTS directory, and refer to the image to check TargetMemberships in the right-hand menu.

main.jsbundle

Hot reloading does not work

ReactNative supports hot reloading, but sometimes it doesn't work by default.
In that case, press Cmd + D to open the debug menu and select Configure Bundler.

Enter the path for the bundle as shown in the image. The default path is as follows:

  • 127.0.0.1:8081

configure_bundler

Summary

In this article, I explained the setup procedure for ReactNative + TypeScript.
Although there might be slight differences in versions, this is the same construction method I used for smartphone apps I've developed in the past, so I believe it's quite practical.

If there is demand, I plan to publish hands-on guides for app development combining various libraries in the future.
Since I intend to use this setup as a standard at that time, I will continue to refine it periodically.

Discussion