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?

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

If you want to add TypeScript to the existing react app use below command

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

Rename at least one file from js/jsx to .ts/tsx. For example, rename index.js to index.ts or index.tsx and then start the server. This will generate the tsconfig.json file automatically and you are ready to write React in TypeScript.

Solution 3

You can add typescript to an existing project using one of the following commands:

To add TypeScript to an existing Create React App project, first install it:

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 read more here

But the problem is when you are changing a file except index.js like App.js to App.tsx it gives you an error that module not found error can’t resolve ‘./App’ . The source of this issue is lacking a tsconfig.json file. So by creating one the error would be removed. I recommend using the below tsconfig:

 {
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"],
  "exclude": [
    "node_modules"
  ]
}

Solution 4

There is a recent pull request adding Typescript. It’s not merged yet.

Also, the next major version of CRA will upgrade to Babel 7, which supports Typescript.

So I’d suggest you to wait a few weeks. It should be really easy then to add TS to any CRA project.

Solution 5

This answer would have been the best alternative at the time of writing, but now CRA has built-in TS support. See the top answer. I’ll keep this for historical purposes.


You can use react-app-rewired to add typescript to your project’s webpack config, which is a better alternative to ejecting.

If you’re not specifically sure how to add typescript from there, here’s a guide on how to do that.

Solution 6

Nothing works if decided to add typescript in an existing create-react-app project. Finally, I created a brand new throw-away application using a typescript template and took the following tsconfig.json, and pasted it into my existing project. all good now but it is a weird solution.

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}

Solution 7

To start a new Create React App project with TypeScript, you can run:

npx create-react-app my-app --template typescript

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply