Published on

TSC and TS-node

Authors
  • avatar
    Name
    Shelton Ma
    Twitter

1. tsc

npx tsc 使用tsconfig.json文件, 把 .ts 文件编译成 .js 文件(默认输出到 dist/build/ 目录), 使用node运行

2. ts-node:直接运行 TypeScript

  1. 安装

    pnpm install -D typescript ts-node
    
  2. tsconfig.json

    // tsconfig.json
    {
      "compilerOptions": {
        "target": "ES2020",
        "module": "CommonJS",
        "rootDir": "src",
        "outDir": "dist",
        "strict": true,
        "esModuleInterop": true
      },
      "include": ["src"]
    }
    

3. 热重载ts-node-dev

  1. 安装

    pnpm install -D ts-node-dev typescript
    
  2. 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"
    }
    
  3. 当引用了本地模块, 需要使用tsconfig-paths

    "scripts": {
      "dev": "ts-node-dev --respawn --transpile-only -r tsconfig-paths/register src/index.ts"
    }