Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding TypeScript to existing create-react-app app

I'm having difficulties adding TypeScript to an already existing create-react-app application. Previously I've always added it before starting the project, simply by create-react-app my-app --scripts-version=react-scripts-ts, but that doesn't work now.

The only "solution" I found here is:

  1. Create a temporary project with react-scripts-ts
  2. Copy tsconfig.json, tsconfig.test.json , tslint.json from the temporary project src folder to your project's src folder.
  3. In package.json, change all the references to react-scripts to react-scripts-ts in the scripts section.

This just seems like a weird workaround, and not very efficient. Does anyone know if it's possible to add it in a better way than this?

like image 842
Lydia Avatar asked Feb 24 '18 20:02

Lydia


People also ask

Does create react app use TypeScript?

Create react app is basically a node package that we can simply install. The interesting part about this is that you can use it with TypeScript very easily by leveraging the use of npm or yarn depending on what your package manager preference is.


1 Answers

As of react-scripts 2.10 you can add TypeScript to an existing project or when creating a new project.

existing project

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

...then rename your .js files to .tsx

new project

yarn create react-app my-app --template typescript 

You can read more here.

like image 178
user1843640 Avatar answered Oct 08 '22 12:10

user1843640