验证打字稿,但不构建它

Validate typescript, but don't build it

我有一个名为“__ tests __”的文件夹。

我希望将其验证为打字稿,但我不想构建该文件夹。 (我不希望它转到 dist 文件夹)

我该怎么做?

好像我必须包括它,但不是真的...

我的ts.config.json:

{
    "compilerOptions": {
        "module": "CommonJS",
        "target": "ES2017",
        "noImplicitAny": true,
        "preserveConstEnums": true,
        "outDir": "./dist",
        "sourceMap": true,
        "esModuleInterop": true,
        "resolveJsonModule": true
    },
    "include": ["src/**/*", "src/**/*.json", "__tests__/**/*"],
    "exclude": ["node_modules", "**/*.spec.ts"]
}

您可以在您的用例中简单地使用选项 noEmit 来不发出输出。

{
  "compilerOptions": {
    // ...
    "noEmit": true,
  }
}

仅在测试用例中发射的更新

我认为您还可以创建用于测试文件的新配置 include 只有您的测试代码从当前代码扩展而来。

tsconfig.test.json

{
  "extends": "./tsconfig.json",
  {
   "compilerOptions": {
      "noEmit": true,
    },
   "include": [
      "__tests__", // your test file
   ],
  }
}

package.json

{
  "scripts": {
    "build": "tsc",
    "test:typeCheck": "tsc --project tsconfig.test.json"
  }
}
  • 类型检查你的测试文件夹而不发出:npm run test:typeCheck
  • 运行 构建以正常发出文件:npm build