- Install typescript, @types/react and @types/react-dom.
npm install --save-dev typescript @types/react @types/react-dom
- In packages.json, replace vite build with tsc && vite build.
"build": "vite build"
With 👇
"build": "tsc && vite build"
-
Rename
vite.config.js
andmain.jsx
tovite.config.ts
andmain.tsx
-
Configure TypeScript by creating these two files in the root of your project:
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"]
}
- Create a file named
vite-env.d.ts
inside thesrc/
folder and copy and paste this (with the three slashes at the beginning):
/// <reference types="vite/client" />
- In your index.html you should change the name of your script from the old main.jsx to main.tsx like this:
<script type="module" src="/src/main.tsx"></script>