# Install TypeScript as a dev dependency
npm install typescript --save-dev
# Compile a specific TypeScript file (e.g., app.ts)
npx tsc app.ts
tsc(TypeScript Compiler)自行把 .ts/.tsx 解析成 TS AST,然后做类型检查、产生 .js 或 .d.ts 等输出。tsserver 则在编辑器里作为长驻进程,基于 tsc API 提供 IntelliSense、跳转、重构等语言服务。它用的是同一份 TypeScript Compiler API 的 AST,所以 tsc 和 tsserver“共享”这一套解析/类型引擎。Create a tsconfig.json in your project root. This configuration enables strict type-checking and modern JavaScript features.
{
"compilerOptions": {
"target": "ES2020", // Use modern JS features
"module": "ESNext", // Use the latest module system
"moduleResolution": "node", // Node module resolution
"outDir": "./dist", // Compiled output directory
"rootDir": "./", // Root of your source files
"strict": true, // Enable all strict type-checking options
"esModuleInterop": true, // Allow default imports from non-ES modules
"skipLibCheck": true, // Skip type checking of declaration files
"forceConsistentCasingInFileNames": true, // Enforce consistent file naming
"resolveJsonModule": true, // Import JSON files as modules
"allowSyntheticDefaultImports": true, // Allow default imports even if module doesn't have one
"strictNullChecks": true, // Null and undefined are separate types
"noImplicitAny": true, // Disallow variables with an implied any type
"noUnusedLocals": true, // Error on unused locals
"noUnusedParameters": true, // Error on unused function parameters
"noFallthroughCasesInSwitch": true // Prevent fall-through in switch cases
},
"include": ["src/**/*.ts"],
"exclude": ["node_modules", "dist"]
}