Migrating create-react-app from javascript to typescript

Viewed 22117

I started a react project using create-react-app few months ago and I'm interesting in migrating the project from Javascript to Typescript.

I saw that there is a way to create react app with typescript using the flag:

--scripts-version=react-scripts-ts

But I didn't find any explanation how can I migrate an existing JS project to TS. I need it to be done incrementally so I can work with both .js and .ts files so I can do the transformation over time. Does anyone has any experience with this migration? What are the required steps that should be done to make this work?

4 Answers

Create React App v2.1.0 was released today with TypeScript support today. That means you no longer need to eject or use the react-scripts-ts fork; all you need to do if you have an existing Create React App project is install the required packages:

$ 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

You can then simply rename .js files to .ts files to start using TypeScript.

(If you have an existing app using the create-react-app-typescript fork, here's a tutorial on how to port it to regular Create React App.)

1). run

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

2). Rename any file to be a TypeScript file (e.g. src/index.js to src/index.tsx)

3). run npm i @types/react-dom @types/react-redux @types/react-router-dom

4). Restart your development server!

enjoy :)

Related