在尝试编译我的打字稿源代码时,我看到编译器也在尝试编译我的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"
]
}
当我运行以下命令" tsc -w -p tsconfig.json"我收到以下错误
node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.
答案 0 :(得分:2)
阅读本文后,我得到了答案 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"
]
}
答案 1 :(得分:0)
为什么?
当TypeScript版本不对应时,也会发生这种情况。
例如您正在全局npm缓存中运行2.9.x,并且在项目node_modules中本地安装了TypeScript 3.5.x。
测试
您可以通过运行“ npx tsc”进行测试,但这仅在将TypeScript保存为依赖项,运行“ npm install”并且具有npm 5.2.x或更高版本的情况下有效。
否则,您可以使用“ npm list typescript”检查本地TypeScript版本,并使用“ npm list typscript -g”检查全局TypeScript版本
解决方案
如果通过“ npx tsc”方法通过,或者在第二种版本不匹配的情况下,则只需要确保将本地TypeScript版本与全局TypeScript版本对齐,或者反之亦然。
其他说明:
“ npx”将使用项目的本地“ node_modules”文件夹中的Node包运行命令。
如果您不了解Node Version Manager,那么可以快速切换Node版本。