Tips
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.
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:
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
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.
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,
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>;
};
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.
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!