tsconfig.json的“排除”属性未得到尊重

Ken*_*Ken 7 node.js typescript tsd

我正在使用此处找到的出色的Express / Node / Typescript示例代码。它使用来自run.sh的以下命令来编译.ts代码:

./node_modules/.bin/tsc --sourcemap --module commonjs ./bin/www.ts

这按宣传的方式工作,但是我更喜欢使用tsconfig.json文件,tsc -p .但是,当我运行该命令时,我得到了很多TS2300: Duplicate identifier 'foo' errors时候tsc(错误?)尝试遍历./node_modulesand ./typings目录的信息。以下是我正在使用的tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings"
  ]
}
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?我正在使用tsc 1.7.3 FWIW。

jmu*_*sch 31

同样,我遇到了node_modules排斥问题。

这是一个笨手笨脚的解决方案,它会忽略所有*.d.ts文件。

我补充说compilerOptions

"compilerOptions": {
    "skipLibCheck": true,
    ...
 }
Run Code Online (Sandbox Code Playgroud)

  • 我已经看遍了,这最终对我有用。 (5认同)
  • 尽管名称中有“lib”,“skipLibCheck”[已记录](https://www.typescriptlang.org/docs/handbook/compiler-options.html#compiler-options)可以做一些不同的事情:“跳过类型检查”所有声明文件 (*.d.ts)。`。 (3认同)

小智 18

我发现**/在要排除的目录之前添加两个星号和一个斜杠()可以解决问题,编辑文件tsconfig如下:

{
...
  "exclude": [
    "**/node_modules",
    "**/typings"
  ]
...
}
Run Code Online (Sandbox Code Playgroud)

  • `tsconfig.json` 中的路径是相对于文件本身的。例如,如果您的“node_modules”位于子目录中,则需要指定确切的路径(“path/to/node_modules”),或者放置“**”,这意味着位于任意子目录中。 (2认同)

Sam*_*ade 14

好的,如果您已经尝试了其他所有方法,请检查代码库中是否存在导致“排除”代码的导入语句。如果您在 Typescript 作用域中包含一个从(例如)“compiled-src”导入文件的文件,那么所有导入的文件都会突然被包含在内。这可能会产生多米诺骨牌效应,使排除属性看起来没有受到尊重。

具体来说,我使用 intellisense 从我的代码库中自动导入某些内容,并且 intellisense 决定它更喜欢编译 src 中的文件而不是 typescript 文件。我没有注意到,过了很长时间才意识到发生了什么。


Mic*_*ael 9

我确实看到这是不久前的事了,而且您已经接受了答案,但我想添加以下内容,记录在此处

重要提示:排除由于包含设置而包含的文件的更改

include由于tsconfig 中没有设置,因此该exclude设置不会产生任何效果。


NSj*_*nas 8

Typescript 将拉入import作为项目一部分的文件中的语句引用的任何路径。

如果您看到已“排除”的文件正在被处理,请检查其他代码中对它们的引用。

  • 这对我有用,我一直直接从 node_modules 导入一些文件,但无法弄清楚为什么它们没有被排除。谢谢! (2认同)

Mar*_*cka 1

我做了:

git clone https://github.com/czechboy0/Express-4x-Typescript-Sample.git
cd Express-4x-Typescript-Sample/
./run.sh
tsd install  # I don't know why, but this helped me.
./run.sh
Run Code Online (Sandbox Code Playgroud)

我创建了Express-4x-Typescript-Sample/tsconfig.json包含内容的文件

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings"
  ]
} 
Run Code Online (Sandbox Code Playgroud)

我跑了

[...]/Express-4x-Typescript-Sample$ tsc -p .
Run Code Online (Sandbox Code Playgroud)

它对我有用 - 即没有错误。