Typescript 排除/忽略(不编译) rootDir 中的文件

Jas*_*son 7 configuration compilation transpiler typescript tsc

我正在编写一个 TypeScript/React Web 应用程序,它具有以下顶级目录\n结构:

\n
\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 jest.config.ts\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 node_modules\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package-lock.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 public\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 webpack.config.js\n
Run Code Online (Sandbox Code Playgroud)\n

所有 TypeScript 源文件都包含在该src/目录中,但我想在编译/类型检查中忽略单个文件(将来可能还会有更多文件)。

\n

我在此文件中遇到错误,但我不想在应用程序中使用该文件。\n我也不想删除该文件,因为它是另一个应用程序使用的模块的一部分。

\n

错误

\n
error TS2307: Cannot find module 'x' or its corresponding type declarations.\n
Run Code Online (Sandbox Code Playgroud)\n

你能帮我做这个吗?这是tsconfig.json我正在使用的文件:

\n

tsconfig

\n
{\n  "compilerOptions": {\n    "allowJs": true,\n    "emitDecoratorMetadata": true,\n    "esModuleInterop": true,\n    "experimentalDecorators": true,\n    "forceConsistentCasingInFileNames": true,\n    "jsx": "react",\n    "module": "es6",\n    "moduleResolution": "node",\n    "outDir": "./dist",\n    "rootDir": "./src",\n    "skipLibCheck": true,\n    "strict": true,\n    "target": "es5"\n  },\n  "exclude": [\n    "dist/",\n    "jest.config.ts",\n    "webpack.config.js"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我尝试过的

\n

我读到该exclude指令不适用于rootDir,但我还是尝试了\n以下内容:

\n
Add the path to the file in question to the `exclude` array.\n
Run Code Online (Sandbox Code Playgroud)\n

那不起作用(我真的没想到它会起作用)。因此,虽然文件路径仍在数组中exclude

\n
Stop using the `rootDir`, instead add `include: ["src/"]`.\n
Run Code Online (Sandbox Code Playgroud)\n

没有变化,我仍然在我不想编译的文件上收到错误。

\n

从 CLI

\n

我尝试excludeFiles从命令行使用项目根目录和 rootDirtsconfig 中的相对路径\n。

\n
tsc -w --noEmit --excludeFiles path/to/file.ts\n
Run Code Online (Sandbox Code Playgroud)\n

仅按名称排除文件。

\n
tsc -w --noEmit --excludeFiles file.ts\n
Run Code Online (Sandbox Code Playgroud)\n

排除文件的目录(项目根目录的路径& rootDir)。:

\n
tsc -w --noEmit --excludeDirectories path/to/containing/directory\n
Run Code Online (Sandbox Code Playgroud)\n

搜索

\n

我还尝试了大量的网络搜索,并且我搜索了这个网站。

\n

@ts-忽略

\n

注释// @ts-ignore可以消除错误,但我认为这对于我的用例来说不是一个好的解决方案(因为此更改将提交回模块,并且在其他项目中我希望引发错误)。

\n

TIA

\n

Jas*_*son 0

这就是我为实现目标所做的事情。

\n

在下面的示例中,我要编译src/目录中除一个 ( exclude.ts) 之外的所有文件。

\n

设置:

\n
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 node_modules\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 typescript\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 package-lock.json\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 src\n\xe2\x94\x82   \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 exclude.ts\n\xe2\x94\x82   \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 include.ts\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n
Run Code Online (Sandbox Code Playgroud)\n

我的tsconfig.json

\n
{\n  "compilerOptions": {\n    "esModuleInterop": true,\n    "forceConsistentCasingInFileNames": true,\n    "module": "commonjs",\n    "outDir": "dist",\n    "skipLibCheck": true,\n    "strict": true,\n    "target": "es2016"\n  },\n  "exclude": [\n    "src/exclude.ts"\n  ],\n  "include": [\n    "src/"\n  ]\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这有效。当我运行 TypeScript 编译器时,我只能看到目录include.js中的文件dist/。我想我以前尝试过(正如我在之前的问题中所述),但我一定错过了一些东西。

\n