- Published on
TSC and TS-node
- Authors
- Name
- Shelton Ma
1. tsc
npx tsc
使用tsconfig.json
文件, 把 .ts
文件编译成 .js
文件(默认输出到 dist/
或 build/
目录), 使用node运行
2. ts-node:直接运行 TypeScript
安装
pnpm install -D typescript ts-node
tsconfig.json
// tsconfig.json { "compilerOptions": { "target": "ES2020", "module": "CommonJS", "rootDir": "src", "outDir": "dist", "strict": true, "esModuleInterop": true }, "include": ["src"] }
ts-node-dev
3. 热重载安装
pnpm install -D ts-node-dev typescript
script
// --respawn 文件修改重启进程 // --transpile-only 跳过类型检查 // --ignore-watch node_modules // --watch .env "scripts": { "dev": "ts-node-dev --respawn --transpile-only --ignore-watch node_modules --watch .env src/index.ts" }
当引用了本地模块, 需要使用
tsconfig-paths
"scripts": { "dev": "ts-node-dev --respawn --transpile-only -r tsconfig-paths/register src/index.ts" }