67

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?

1

10 Answers 10

66

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 in the docs.

If you are having trouble, the following from the troubleshooting section of the docs might help:

Troubleshooting

If your project is not created with TypeScript enabled, npx may be using a cached version of create-react-app. Remove previously installed versions with npm uninstall -g create-react-app or yarn global remove create-react-app (see #6119).

Sign up to request clarification or add additional context in comments.

7 Comments

This works pretty smooth. One thing I experienced when moving from a pre-existing ES-CRA to TS-CRA is having to change ESLINT settings: robertcooper.me/…
What about tsconfig file? Don't we need to add that?
@iRohitBhatia the documentation says "You are not required to make a tsconfig.json file, one will be made for you", but this is only true for new projects as the file is not created for existing projects. what should we do for existing projects... just create one?
digging a litter deeper, "CRA automatically creates an tsconfig.json file for you when it notices a .ts file in your source folder", it will automatically create that file for you.
it doesn't work, Module not found: Error: Can't resolve show up
|
46

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.

3 Comments

if I rename index.js, no error happened, but if I do it to other files, this error show up Module not found: Error: Can't resolve .... Also there is notsconfig.json file generated
@UMR you need to manually initialize typescript npx tsc --init and set "jsx": "react" in tsconfig.json
I still have to add the TypeScript dependencies to the package.json: "@types/jest": "^27.5.2", "@types/node": "^16.18.13", "@types/react": "^18.0.28", "@types/react-dom": "^18.0.11",
27

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"
  ]
}

1 Comment

you dont use --save. but --save-dev
4

Add TypeScript to an existing React 18 project using official guide

  • Add type definitions

    npm install @types/react @types/react-dom --save-dev
    
  • Add tsconfig.json. For example, take a look at the sample app.

    {
      "compilerOptions": {
        "target": "ES2022",
        "module": "ES2022",
        "esModuleInterop": true,
        "forceConsistentCasingInFileNames": true,
        "strict": true,     // This makes noImplicitAny as true
        "lib": [
          "ES2022",
          "dom"             // This is important
        ],
        "jsx": "preserve"   // This is important
      }
    }
    
  • Change filename extensions. If you have JSX in your file, change its extension to .tsx, otherwise change it to .ts.

    For example:

    • Change App.js to App.tsx
    • Change index.js to index.tsx
  • Start the app

    npm start
    
  • Fix type errors

    For example: This error in index.tsx

    image

    For eg: use non-null assertion operator

    const rootElement = document.getElementById("root")!;
    

Your app should work just fine at this point.

Comments

2

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.

1 Comment

CRA 2 has been released, but it seems that support for Typescript has been postponed: news.ycombinator.com/item?id=18118349
2

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.

1 Comment

It seems ejecting is not necessary anymore.
2

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"
  ]
}

2 Comments

It is not generating tsconfig.json for me either.
I had to do this too. Before, it would only work if I only changed src/index.js to src/index.tsx. If I changed anything else, it would break. Needed to add a tsconfig file myself.
0
yarn add typescript @types/node @types/react @types/react-dom @types/jest

then Create a tsconfig.json file in root folder

 {
  "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"
  ]
}

Comments

-1

After checking your issue I created a template using React, Typescript, Tailwind and Storybook

https://github.com/dsilva2401/react-typecript-tailwind-storybook

2 Comments

Your answer should inform the asker how to solve a problem.
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
-7

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

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

2 Comments

The question literally states that he has an existing project that he'd like to convert.
haji enjoori rotbe nemigiri.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.