Education

Bootcamps

Remote live bootcamps to lead your career onto a new path.

Web development 24 weeks

Self-study

Access all the materials from our live bootcamps and study on your own.

Web development

Courses

Upgrade your career with new skills while staying at work.

Stories
Resources
Events
For companies

Courses

Training courses for your organisation.

Training courses

Connect

Meet our graduates and build your future talent pool.

Meet our students
About us
Education

Bootcamps

Remote live bootcamps to lead your career onto a new path.

Web development 24 weeks

Self-study

Access all the materials from our live bootcamps and study on your own.

Web development

Courses

Upgrade your career with new skills while staying at work.

popcorn
Stories
Resources
Events
For companies

Courses

Training courses for your organisation.

Training courses

Connect

Meet our graduates and build your future talent pool.

Meet our students
popcorn
About us
Joanna Philips

Tips

Convert React JavaScript to TypeScript: a Step-by-Step Guide

Joanna Philips
Javascript Developer & Technigo graduate
LinkedIn

TypeScript is a superset of JavaScript that brings static typing to your code, helping catch errors early and making your codebase more maintainable. If you're looking to migrate your existing React JavaScript project to TypeScript you might consider using a JavaScript to TypeScript converter if you want more simple codebase. If you want to have more control and customization, use this step-by-step guide to get you started on your IDE.

Step 1: Understand TypeScript

Before diving into the migration process, take some time to understand the basics of TypeScript. Learn about TypeScript's type system, interfaces, and how it differs from JavaScript. 

Some benefits of migrating to TypeScript:

  • Reduces bugs in your code
  • Improves the integration of new contributors
  • Reduces the need for some types of tests
  • Projects are often easier to maintain

Step 2: Set Up TypeScript in Your Project

Start by adding TypeScript to your project. Install TypeScript in your root folder as a dev dependency using your package manager.

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

Step 3: Create a tsconfig.json File

A tsconfig.json file is crucial for configuring TypeScript options;  it allows you to customize TypeScript's behaviour for your project. Run the command below to generate a basic tsconfig.json file:

npx tsc --init

Since we are using React in this project, we will have to specify that in the tsconfig.json file as below:

"jsx": "react-jsx"

*Delete the jsconfig.json file (if your project has one) since we now have a tsconfig.json file.

Step 4: Rename Files to .tsx

Rename your React component files from .js or .jsx to .tsx. This change allows TypeScript to recognize JSX syntax within these files. Consider a gradual approach by migrating components one by one. In order for TypeScript to allow importing .js or .jsx files and to allow error reporting on them, we will need to enable the following compiler options in the tsconfig.json file:

"allowJs": true,
"checkJs": true,

Step 5: Type Annotations

Start adding type annotations to your code. Begin with the props and state of your React components. Below is an example of the before and after of the AlbumName.tsx file:

Before: AlbumName.js

exportconst AlbumName = (props) => {
return <p className="albumName">{props.albumNamesInput}</p>;
};

After: AlbumName.tsx
type AlbumNamesInputProps = {
  albumNamesInput: string;
}
export const AlbumName = ({ albumNamesInput } : AlbumNamesInputProps) =>
{
  return <p className="albumName"> {albumNamesInput} </p>;
};

Additional steps

Configuring ESlint for TypeScript

If you are using ESlint in your project, make sure the configurations on the .eslintrc.json file is as below:

"parser": "@typescript-eslint/parser",
  "plugins": [
    "@typescript-eslint"
  ],
  "extends": [
    "eslint:recommended",
    "plugin:@typescript-eslint/eslint-recommended",
    "plugin:@typescript-eslint/recommended"
  ]
}

Handling Third-Party Libraries

Some third-party libraries might not have TypeScript definitions by default. Look for community-contributed type definitions on DefinitelyTyped or create your own type declarations in a d.ts file. Read more about it on the documentation.

Conclusion

Remember that migrating to TypeScript is a journey, and taking your time to learn and adapt is okay. Migrating an entire codebase at once can be overwhelming, so start with one component at a time. With each component you migrate, you'll gain a better understanding of TypeScript and its benefits. Happy coding!

We're a female-founded, remote-first community helping people get a career they love. 90% of those attending our boot camps are women.

Our next bootcamps start in August - apply today to secure a place!

👉 Web Development Boot Camp

👉 UX Design Boot Camp