How to add typescript to an existing vite react app? #6799
-
Hi, I would really appreciate some solution as I couldn't find any link regarding this in the docs. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 21 replies
-
Hello, did you find a solution to this? |
Beta Was this translation helpful? Give feedback.
-
I would just scaffold a new Vite project using the |
Beta Was this translation helpful? Give feedback.
-
!. install typescript, @types/react and @types/react-dom. tsconfig.json {
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
} tsconfig.node.json {
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
} pretty easy to do. I wonder why no one made a response like this. HAPPY CODING :) |
Beta Was this translation helpful? Give feedback.
-
This is a summary of my previous response, and the response of @segfaulty1, in order to have everything in one single place. Step 1/6 npm install -D typescript @types/react @types/react-dom Step 2/6
With 👇
Step 3/6 Rename Step 4/6
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": true,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
{
"compilerOptions": {
"composite": true,
"module": "ESNext",
"moduleResolution": "Node",
"allowSyntheticDefaultImports": true
},
"include": ["vite.config.ts"]
} Step 5/6 Create a file named
Step 6/6 In your <script type="module" src="/src/main.tsx"></script> That's it. Happy coding 🙂 |
Beta Was this translation helpful? Give feedback.
-
I am using npm run dev in vite. Do I have to change something in scripts and do I have to first build before I run dev? |
Beta Was this translation helpful? Give feedback.
-
Did you also rename main.jsx file to a .tsx file? I renamed it and am getting exceptions for many of my imports there:
Also what changes did you have to make for eslint ?? And the above steps will allow javascript as well so I can transition slowly to TS? Or do I need to convert everything in the project to tsx for this to work |
Beta Was this translation helpful? Give feedback.
-
Just needed this today and found this ticket. I am providing some additional notes. While this all helped to get me going, YMMV, don't make random changes, plan to revert if required.
Modify the default From : HTH |
Beta Was this translation helpful? Give feedback.
-
When i run the build, the build folder is not created. The compiled files are going to inside the folder "src" |
Beta Was this translation helpful? Give feedback.
!. install typescript, @types/react and @types/react-dom.
2. in packages.json, replace
vite build
withtsc && vite build
.3. configure your TS:
tsconfig.json