【问题标题】:How to exclude files from being processed by tsc?如何排除文件被 tsc 处理?
【发布时间】:2025-11-26 13:00:01
【问题描述】:

如何阻止 tsc 处理另一个需要的 Javascript 文件?

我想对我的主要index.js 进行全面检查,但它requires() 是由emcc 创建的generated.js Javascript 文件,这很好,但没有通过很多tsc' s 检查。

我尝试将文件添加到我的 tsconfig.json 的排除列表中,例如:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "lib": [
        "dom",
        "webworker"
    ],
    "allowJs": true,
    "checkJs": true,
    "outDir": "./dist",
    "noImplicitAny": false,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    "index.js"
  ],
  "exclude": [
    "generated.js"
  ]
}

但这没有任何效果。当我运行tsc --build tsconfig.json 时,我从generated.js 中听到了一些错误提示。

【问题讨论】:

  • 如果你能在编译时以某种方式在generated.js 之上获得// @ts-nocheck,我认为这是最简单的做法
  • @apokryfos 你是对的。那行得通。

标签: javascript typescript tsc


【解决方案1】:

tsconfig.json 文件默认查找 typescript 文件,而您提供.js 文件。您可以将文件扩展名从 .js 更改为 .ts 以让 typescript 照顾它们。所以你的tsconfig.json 文件可能最终看起来像这样:

{
  "compilerOptions": {
    "target": "es5",
    "module": "system",
    "lib": [
        "dom",
        "webworker"
    ],
    "allowJs": true,
    "checkJs": true,
    "outDir": "./dist",
    "noImplicitAny": false,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "include": [
    "index.ts"
  ],
  "exclude": [
    "node_modules",
    "generated.ts"
  ]
}

【讨论】:

  • 但我的文件不是 Typescript。他们是Javascript。我使用其他工具(例如 browserfy)来捆绑部署的所有内容,它不接受 Typescript。
  • @Cerin 如果您的index.js 不是生成的文件,您可以在tsconfig.json 中设置"checkJs: false" 并使用index.ts 将项目的打字稿部分分离到另一个目录并让打字稿编译到您想要的目录(成为打字稿的dist 文件夹)中的等效index.js,browserify 从中读取以进行捆绑。