typescript tsconfig.json的“exclude”属性未得到尊重

vohkndzv  于 2023-05-19  发布在  TypeScript
关注(0)|答案(9)|浏览(371)

我正在使用优秀的Express/Node/Typescript示例代码here。它使用来自www.example.com的以下命令转换.ts代码run.sh:
./node_modules/.bin/tsc --sourcemap --module commonjs ./bin/www.ts
这和广告上说的一样,但是我更喜欢使用tsconfig.json文件和tsc -p .。然而,当我运行这个命令时,我得到了大量的TS2300: Duplicate identifier 'foo' errors,而tsc(错误地?)尝试遍历./node_modules./typings目录。下面是我使用的tsconfig.json:

{
  "compilerOptions": {
    "target": "ES5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false
  },
  "exclude": [
    "node_modules",
    "typings"
  ]
}

我用的是tsc 1.7.3 FWIW。

cgyqldqp

cgyqldqp1#

同样,我也遇到了node_modules排除的问题。
这里有一个笨拙的解决方案,它忽略了所有的*.d.ts文件。
compilerOptions中添加:

"compilerOptions": {
    "skipLibCheck": true,
    ...
 }

相关:

  • https://www.typescriptlang.org/tsconfig#compilerOptions
yfwxisqw

yfwxisqw2#

Typescript将拉入项目文件中import语句引用的任何路径。
如果您看到正在处理的文件是您已经“排除”的,那么请检查其他代码中对它们的引用。

bvhaajcl

bvhaajcl3#

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

{
...
  "exclude": [
    "**/node_modules",
    "**/typings"
  ]
...
}
toiithl6

toiithl64#

好的,如果你已经尝试了其他所有方法,检查一下你的代码库中没有导入导致“排除”代码的语句。如果您在Typescript作用域中包含一个从(例如)“compiled-src”导入文件的文件,则导入的所有文件将突然被包含在内。这可能会产生多米诺骨牌效应,使exclude属性看起来没有得到尊重。
具体来说,我使用intellisense从我的代码库中自动导入一些东西,intellisense决定优先使用compiled-src中的文件而不是typescript文件。我没有注意到,过了很久才意识到发生了什么。

ssm49v7z

ssm49v7z5#

我确实看到这是一段时间以前的事了,而且你已经接受了一个答案,但我想补充以下内容,这里有记录:
“**重要提示:**exclude only 会更改由于include设置而包含的文件。”
由于tsconfig中没有include设置,因此exclude设置不会产生任何影响。

5cnsuln7

5cnsuln76#

我也有同样的问题。把头发拔了大约30分钟,然后发现如果我改变:

"target": "ES5",

"target": "ES6",

所有的错误都消失了!

1mrurvl1

1mrurvl17#

我做到了:

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

我创建了包含内容的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"
  ]
}

我跑了

[...]/Express-4x-Typescript-Sample$ tsc -p .

而且它能让我工作--也就是说。不存在错误。

tpgth1q7

tpgth1q78#

我遇到了这个问题,我的exclude看起来像这样:

"exclude": [
  "node_modules",
  "typings"
]

当我删除"typings"时,它起作用了。我的最终解决方案:

"exclude": [
  "node_modules"
]
j9per5c4

j9per5c49#

04/02/0217(4月2日)-我正在经历同样的事情,几乎花了整整一个周末。最后,我找到了这个网站(我从来没有见过任何stackoverflow帖子链接):https://angular.io/docs/ts/latest/guide/typescript-configuration.html
在其中,我发现了这行,就在编译器选项中:

"lib": [ "es2015", "dom" ]

我不知道它是做什么的,在这一点上我不在乎,但我所有的node_modules错误都消失了。
至于include/exclude不起作用,我相信这是因为“依赖关系”。即使您排除了一个文件夹,如果导入的文件(如Component或NgModule)对node_modules中的文件有一些依赖关系,tsc将尝试编译该文件。

相关问题