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\nRun Code Online (Sandbox Code Playgroud)\n所有 TypeScript 源文件都包含在该src/目录中,但我想在编译/类型检查中忽略单个文件(将来可能还会有更多文件)。
我在此文件中遇到错误,但我不想在应用程序中使用该文件。\n我也不想删除该文件,因为它是另一个应用程序使用的模块的一部分。
\nerror TS2307: Cannot find module 'x' or its corresponding type declarations.\nRun Code Online (Sandbox Code Playgroud)\n你能帮我做这个吗?这是tsconfig.json我正在使用的文件:
{\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}\nRun Code Online (Sandbox Code Playgroud)\n我读到该exclude指令不适用于rootDir,但我还是尝试了\n以下内容:
Add the path to the file in question to the `exclude` array.\nRun Code Online (Sandbox Code Playgroud)\n那不起作用(我真的没想到它会起作用)。因此,虽然文件路径仍在数组中exclude:
Stop using the `rootDir`, instead add `include: ["src/"]`.\nRun Code Online (Sandbox Code Playgroud)\n没有变化,我仍然在我不想编译的文件上收到错误。
\n我尝试excludeFiles从命令行使用项目根目录和 rootDirtsconfig 中的相对路径\n。
tsc -w --noEmit --excludeFiles path/to/file.ts\nRun Code Online (Sandbox Code Playgroud)\n仅按名称排除文件。
\ntsc -w --noEmit --excludeFiles file.ts\nRun Code Online (Sandbox Code Playgroud)\n排除文件的目录(项目根目录的路径& rootDir)。:
tsc -w --noEmit --excludeDirectories path/to/containing/directory\nRun Code Online (Sandbox Code Playgroud)\n我还尝试了大量的网络搜索,并且我搜索了这个网站。
\n注释// @ts-ignore可以消除错误,但我认为这对于我的用例来说不是一个好的解决方案(因为此更改将提交回模块,并且在其他项目中我希望引发错误)。
TIA
\n这就是我为实现目标所做的事情。
\n在下面的示例中,我要编译src/目录中除一个 ( exclude.ts) 之外的所有文件。
设置:
\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\nRun Code Online (Sandbox Code Playgroud)\n我的tsconfig.json
{\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}\nRun Code Online (Sandbox Code Playgroud)\n这有效。当我运行 TypeScript 编译器时,我只能看到目录include.js中的文件dist/。我想我以前尝试过(正如我在之前的问题中所述),但我一定错过了一些东西。
| 归档时间: |
|
| 查看次数: |
6372 次 |
| 最近记录: |