DEV Community

Cover image for Getting started with TypeScript + React
Bruno
Bruno

Posted on

Getting started with TypeScript + React

Starting a project in React with create-react-app may be the easiest way to start a new project, but what if you want to use TypeScript instead of JavaScript?

puppet thinking

Don’t worry, we will go through this step by step and it will become easy to apply this to your own project by the end of this article.

Starting a new React project

The first step is to create a new React project, which you can do by using the create-react-app approach. Run the following command:



npx create-react-app <project-name>


Enter fullscreen mode Exit fullscreen mode

How to configure your React app to use TypeScript instead of JavaScript

First, let's install TypeScript in our project by running the following commands:

NPM



npm install --save typescript @types/node @types/react @types/react-dom @types/jest


Enter fullscreen mode Exit fullscreen mode

Yarn



yarn add typescript @types/node @types/react @types/react-dom @types/jest


Enter fullscreen mode Exit fullscreen mode

Change the file extensions to TypeScript

In order to write TypeScript code in a React project, rename the files with .js or .jsx into .tsx.

files on vscode

This will now add TypeScript typing to your previously React code.

Create the tsconfig.json file

In order to compile TypeScript, you will need a tsconfig.json file. This JSON format file contains the compiler options for TypeScript.



{
    "include": [
        "src/*"
    ],
    "compilerOptions": {
        "target": "es5",
        "jsx": "react",
        "allowSyntheticDefaultImports": true
    }
}


Enter fullscreen mode Exit fullscreen mode

Alternative from the command line

You can also generate the file using the following command, but first lets make sure that you have TypeScript installed globally:



tsc -v


Enter fullscreen mode Exit fullscreen mode

If not, run the following commands.

NPM



npm install -g typescript 


Enter fullscreen mode Exit fullscreen mode

Yarn



yarn global add typescript


Enter fullscreen mode Exit fullscreen mode

Then, run the following command to generate the tsconfig.json file.

NPM



tsc --init


Enter fullscreen mode Exit fullscreen mode

Yarn



yarn tsc --init


Enter fullscreen mode Exit fullscreen mode

After finishing generating the tsconfig.json file using the above tsc commands, make sure you have it on the root folder of the project and that it looks something like the following:

tsconfig.json file code

πŸŽ‰ Now you should be good to go and create your first TypeScript React component.

Creating your first TypeScript React component

Now that TS is correctly set up and working, it's time to create your first .tsx component.

πŸͺš First, let's create the component.

MyButton.tsx

MyButton.tsx component

Import MyButton.tsx to App.tsx

App.tsx component with MyButton component import

TypeScript-React typing

One of the main TypeScript funcionality that can be used in React components is the typing of props, which are basically the "properties" of your component.

Interface props

In order to define props with TypeScript in React, you can define using an interface.



interface MyButtonProps {
   emoji: string
}


Enter fullscreen mode Exit fullscreen mode

Now, use the props inside the MyButton.tsx component:

MyButton.tsx component with props defined on interface

Now the component contains the props defined inside the MyButtonProps interface.

Typing and debugging

Now that you have created the props and added a type for emoji, unless you add that prop (emoji) to the component, TypeScript will throw you an error such as the following and will not render the UI unless you fix it:

typescript error

This tells you that you are missing that type prop on the component. Add it in order to get rid of this error:

react component with type prop

After adding the missing emoji type prop, when you click on the button, it should display an alert like this with the string passed in the MyButton prop.

emoji displayed in alert popup after button is clicked

πŸŽ‰ Congrats! You have just created your first TypeScript + React component!

πŸ“š Further reading

For further reading on these two frontend tools, the following documentation will be useful:

πŸ‘‹ Thank you for reading!

Now that you have learned the basics of how to get started with TypeScript in React projects, you can take these learnings and apply to your projects, allowing you to build more complex and maintainable React code. I will see you on my next article!

Top comments (6)

Collapse
 
brense profile image
Rense Bakker

Whats wrong with the already provided typescript template from CRA?
npx create-react-app my-app --template typescript

Collapse
 
bcostaaa01 profile image
Bruno

There is nothing wrong with the already provided template for create-react-app TypeScript πŸ˜‰ it works absolutely fine. The point with this approach is also for the case that we would go about adding TS to an already existing existing React project. Thank you for pointing out this aspect!πŸ™‚

Collapse
 
brense profile image
Rense Bakker

Ah yes then it makes more sense! Thanks for the clarification πŸ‘ Make sure to also check out vite's guide for React + Typescript (vite is really nice and much faster than CRA/webpack).

Thread Thread
 
bcostaaa01 profile image
Bruno

Thank you for the suggestion, Rense!πŸ™‚I will look into it and share my thoughts in a future article!

Collapse
 
darkmix841 profile image
Info Comment hidden by post author - thread only accessible via permalink
DarkMix842

I did not understand anything. Please write more clearly.

Collapse
 
bcostaaa01 profile image
Bruno

I wrote the article as clearly as possible, not only in words but also visually, so I do not understand which part/s you did not understand, as you have not mentioned the points you found unclear… I would appreciate if you would have mentioned them!

Some comments have been hidden by the post's author - find out more