Convert existing JavaScript project to support TypeScript
1. Install typescript
pnpm add -D typescript @types/node ts-node
2. Create TypeScript config file
npx tsc --init
And after that, you can use the following config:
{
"compilerOptions": {
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"target": "ES2020",
"skipLibCheck": true,
"useDefineForClassFields": true,
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"module": "ESNext",
"outDir": "dist",
"allowJs": true,
"incremental": true,
/* Bundle mode */
"moduleResolution": "Bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"noEmit": true,
"isolatedModules": true,
/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
/* Path */
"baseUrl": ".",
"paths": {
"@/*": ["./src/*"]
}
},
"include": ["src"],
"exclude": ["node_modules", "dist"]
}
3. Update package.json
package.json
{
...
"scripts": {
"compile": "npx tsc -p ./",
"watch": "npx tsc -watch -p ./"
}
...
}