This content originally appeared on Bits and Pieces - Medium and was authored by Suphi Başdemir
The basics of setting up a React project with TypeScript
Introduction:
Explanation of what React, TypeScript, and Create React App are:
React is a popular JavaScript library for building user interfaces. It allows you to build reusable UI components and manage the state of your application. TypeScript is a typed superset of JavaScript that compiles to plain JavaScript. It allows you to add type checking to your JavaScript code, making it more robust and easier to maintain. Create React App (CRA) is a tool that sets up a new React project with a basic file structure and default configurations.
B. Explanation of why you might want to use TypeScript in a React project:
TypeScript can help you to catch errors early, making it easier to find and fix bugs in your code. It also improves the readability of your code by providing you with clear type errors and giving you better tooling and auto-completion suggestions. Using TypeScript in a React project can also lead to fewer runtime errors, which makes your code more robust, and helps you to ship features faster.
C. Overview of the steps involved in setting up a React project with TypeScript:
The process of setting up a React project with TypeScript involves a few steps, including installing Create React App, creating a new React project with TypeScript, integrating TypeScript into an existing React project, and using TypeScript in your React project. Throughout this guide, we will walk you through each of these steps, providing you with clear instructions, tips and best practices for using TypeScript in a React project.
Creating a new React project with TypeScript:
A. Instructions on how to create a new React project with TypeScript:
To create a new React project with TypeScript, you first need to have Create React App installed on your computer, as outlined in the previous section. Once you have Create React App installed, you can use the following command to create a new React project with TypeScript:
npx create-react-app my-app --template typescript
This command will create a new directory called my-app and set up a new React project with TypeScript.
The --template redux-typescript flag tells the create-react-app tool to use a template that includes support for both Redux and TypeScript. This template will include some additional configuration, such as setting up the @reduxjs/toolkit and react-redux packages and configuring the TypeScript compiler to work with the project.
B. Explanation of the file structure and default configurations of the new project:
Once you have created your new React project with TypeScript, you should see the following file structure:
my-app/
node_modules/
public/
src/
components/
index.tsx
react-app-env.d.ts
package.json
package-lock.json
README.md
tsconfig.json
tslint.json
- The public directory contains the HTML file that is used to render your application.
- The src directory contains the JavaScript and TypeScript files that make up your application.
- tsconfig.json and tslint.json files are used to configure TypeScript in your project.
- package.json and package-lock.json files are used to manage the dependencies of your project.
C. Tips on how to verify that TypeScript is working correctly in the new project:
- You can run your application in development mode with the following command:
npm start
or
yarn start
This will start a development server that you can use to test your application.
- You can check that TypeScript is working correctly by trying to import a type or interface in one of your components, if you don’t get any errors and it’s working, the TypeScript is configured correctly.
- You can also check for typescript errors by running:
npm run build
or
yarn build
This command will build your application and check for TypeScript errors.
There’s also an option to add TypeScript linter, such as tslint, eslint with @typescript-eslint/parser to check for TypeScript errors as well as coding conventions, this will give you more complete check for your codebase.
- TypeScript is also able to provide you with better tooling and autocompletion suggestions when you are writing code, by using an editor with TypeScript integration like VSCode, you should be able to see the benefits of using TypeScript in your project.
Integrating TypeScript into an existing Create React App (CRA) project:
A. Explanation of why you might want to add TypeScript to an existing React project:
As your project grows, it can become harder to keep track of all the state, props, and types of your components. TypeScript can help you to catch errors early, making it easier to find and fix bugs in your code. It also improves the readability of your code by providing you with clear type errors and giving you better tooling and auto-completion suggestions. With TypeScript, you can also leverage features like interfaces and decorators that help to make your code more maintainable and expressive.
B. Instructions on how to add TypeScript to an existing React project:
To add TypeScript to an existing Create React App (CRA) project, you can follow these steps:
- Install the necessary dependencies:
npm install --save typescript @types/node @types/react @types/react-dom @types/jest
or
yarn add typescript @types/node @types/react @types/react-dom @types/jest
Convert the project to use TypeScript:
npx react-scripts --scripts-version=react-scripts-ts
Using TypeScript in your React project:
A. Explanation of how to use TypeScript types and interfaces in a React project:
TypeScript Types:
- TypeScript provides a number of built-in types, such as string, number, boolean, and array. You can use these types to specify the types of variables, function arguments, and function return values. For example, you can specify that a variable is of type string like this:
let name: string = "John";
Or, you can specify that a function takes an argument of type number and returns a value of type string:
function multiply(a: number, b: number): string {
return (a * b).toString();
}TypeScript Interfaces:
- An interface in TypeScript is a way to define a contract for the shape of an object. It can be useful for specifying the expected properties and methods of an object, and for creating contracts between different parts of your code. For example, you can define an interface for a person object like this:
interface Person {
firstName: string;
lastName: string;
age: number;
}Using TypeScript types and interfaces in React:
- When using TypeScript in a React project, you can use types and interfaces to specify the types of props and state in your components. For example, you can specify that a component expects a prop of type number like this:
interface Props {
age: number;
}and then use it in your component like this:
type Props = { age: number };
const MyComponent = (props: Props) => {
return <p>My age is {props.age}</p>;
};B. Best practices for using TypeScript in a React project:
- Start by adding types to the most important parts of your application, like the props and state of your components, and the arguments and return values of your functions.
- Use the interfaces and types to shape your data and state in your components.
- Try to be as explicit as possible when defining types and interfaces. This can make it easier to understand how different parts of your code interact with one another.
- Leverage TypeScript’s features such as union types, interfaces, type guards, and discriminated unions to make your code more robust and expressive.
- Consider using a linter like TSLint to catch any issues with your TypeScript code and enforce your team’s coding conventions.
C. Tips on how to debug TypeScript in a React project:
- Use a code editor with TypeScript integration, like VSCode, which can provide you with real-time type checking and error highlighting, making it easier to find and fix errors in your code.
- Make sure that you have the TypeScript compiler configured correctly, by double-checking that your tsconfig.json file is set up correctly.
- Take advantage of the TypeScript compiler’s error messages, which are designed to be helpful and descriptive. Pay attention to the line numbers and error messages, as they can provide valuable information about what went wrong.
- Make use of the console.log() and the browser’s developer tools to understand how your variables and components behave.
Conclusion:
A. Summary of the steps involved in setting up a React project with TypeScript:
The process of setting up a React project with TypeScript involves installing Create React App, creating a new React project with TypeScript, integrating TypeScript into an existing React project, and using TypeScript in your React project. These steps include installing necessary dependencies, converting a project to TypeScript, specifying the types of props and state in your components, and using interfaces to define the shape of your objects.
B. Additional resources for learning more about using TypeScript in a React project:
- The TypeScript documentation: https://www.typescriptlang.org/docs
- The React documentation: https://reactjs.org/docs
- The Create React App documentation: https://create-react-app.dev/docs/getting-started
- The GitHub repository for the TypeScript-React-Starter: https://github.com/Microsoft/TypeScript-React-Starter
C. Call to action for readers to start their own React projects with TypeScript:
Now that you’ve learned the basics of setting up a React project with TypeScript, it’s time to put your new skills to the test. We encourage you to start your own React project with TypeScript and see for yourself the benefits of using a typed language in your development workflow. Happy coding!
Build React apps with reusable components, just like Lego

Bit’s open-source tool help 250,000+ devs to build apps with components.
Turn any UI, feature, or page into a reusable component — and share it across your applications. It’s easier to collaborate and build faster.
Split apps into components to make app development easier, and enjoy the best experience for the workflows you want:
→ Micro-Frontends
→ Design System
→ Code-Sharing and reuse
→ Monorepo
Learn more:
- How We Build Micro Frontends
- How we Build a Component Design System
- How to reuse React components across your projects
- 5 Ways to Build a React Monorepo
- How to Create a Composable React App with Bit
How to Set up a React Project with TypeScript was originally published in Bits and Pieces on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Bits and Pieces - Medium and was authored by Suphi Başdemir
Suphi Başdemir | Sciencx (2023-02-17T17:11:20+00:00) How to Set up a React Project with TypeScript. Retrieved from https://www.scien.cx/2023/02/17/how-to-set-up-a-react-project-with-typescript/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.