即使在排除它们之后,打字稿也会在 node_modules 下编译类型

Rah*_*uly 5 typescript tsconfig

在尝试编译我的打字稿源代码时,我看到编译器也在尝试编译我的 node_modules 文件夹下的类型。我正在使用 typescript 2.6.1,我的 tsconfig 文件如下

 {
  "compilerOptions": {
  "allowSyntheticDefaultImports":true,
  "outDir": "./dist",
  "mapRoot": "./dist",
  "module": "commonjs",
  "target": "es6",
  "sourceMap": true,
  "sourceRoot": "./source",
  "removeComments": false
},
"exclude": [
  "node_modules",
  "test"
],
  "include": [
  "source/*.ts"
]
}
Run Code Online (Sandbox Code Playgroud)

当我运行以下命令“tsc -w -p tsconfig.json”时,出现以下错误

 node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.
Run Code Online (Sandbox Code Playgroud)

Rah*_*uly 8

看完这篇文档,我得到了答案 https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

在@types、typeRoots 和类型部分下

他们提到

指定 "types": [] 以禁用自动包含 @types 包。

更新后的 tsconfig.json 如下

{
  "compilerOptions": {
   "allowSyntheticDefaultImports":true,
   "outDir": "./dist",
   "mapRoot": "./dist",
   "module": "commonjs",
   "target": "es6",
   "sourceMap": true,
   "sourceRoot": "./source",
   "removeComments": false,
   "types": []
 },
 "exclude": [
   "node_modules",
   "test"
 ],
 "include": [
   "source/*.ts"
 ]
}
Run Code Online (Sandbox Code Playgroud)