【问题标题】:Typescript typings - Duplicate name Promise / Cannot find name Promise打字稿打字 - 重复名称承诺/找不到名称承诺
【发布时间】:2017-03-08 08:34:07
【问题描述】:

我正在构建一个客户端/服务器 JS 应用程序,但在使用 Promises 时遇到了一个大问题。它们要么是未定义的,要么是重复的,而且它似乎依赖于 @types 包。

npm install --save @types/es6-promise`

这会产生如下的服务器错误:

cd ../server 
➜  server git:(master) ✗ tsc
../node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.
../node_modules/@types/es6-promise/index.d.ts(42,19): error TS2300: Duplicate identifier 'Promise'
.../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(4936,11): error TS2300: Duplicate identifier 'Promise'.
../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(5261,11): error TS2300: Duplicate identifier 'Promise'.
../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(5511,13): error TS2300: Duplicate identifier 'Promise'.
../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(5737,11): error TS2300: Duplicate identifier 'Promise'.
➜  server git:(master) ✗ 

如果我删除该软件包,则会收到客户端错误:

tsc
src/components/GeneralChatAdminInputArea.tsx(100,14): error TS2304: Cannot find name 'Promise'.
src/components/GeneralChatAdminInputArea.tsx(103,16): error TS2304: Cannot find name 'Promise'.
src/routes/users/index.ts(11,21): error TS2304: Cannot find name 'Promise'.
src/routes/users/index.ts(12,21): error TS2304: Cannot find name 'Promise'.
src/routes/users/index.ts(13,3): error TS2304: Cannot find name 'Promise'.

解决办法是什么?

我的 tsconfig.json 看起来像这样:

{
    "compilerOptions": {
        "sourceMap": true,
        "noImplicitAny": false,
        "target": "es6",
        "jsx": "react",
        "module": "commonjs",
        "moduleResolution": "node",
        "isolatedModules": false,
        "experimentalDecorators": true,
        "emitDecoratorMetadata": true,
        "declaration": false,
        "removeComments": true,
        "noLib": false,
        "preserveConstEnums": true,
        "suppressImplicitAnyIndexErrors": true,
        "noUnusedLocals": true
    },
    "filesGlob": [
        "**/*.ts",
        "**/*.tsx",
        "**/*.tsd"
    ],
    "compileOnSave": true,
    "buildOnSave": false,
    "atom": {
        "rewriteTsconfig": false
    }
}

这里有另一个版本显示问题来来去去

客户端编译正常

➜  author git:(402-compile-errors) ✗ cd client 
➜  client git:(402-compile-errors) ✗ tsc
➜  client git:(402-compile-errors) ✗ cd ../server 

但服务器出现故障

➜  server git:(402-compile-errors) ✗ tsc
../node_modules/@types/es6-promise/index.d.ts(11,15): error TS2300: Duplicate identifier 'Promise'.
../node_modules/@types/es6-promise/index.d.ts(42,19): error TS2300: Duplicate identifier 'Promise'.
../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(4936,11): error TS2300: Duplicate identifier 'Promise'.
../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(5261,11): error TS2300: Duplicate identifier 'Promise'.
../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(5511,13): error TS2300: Duplicate identifier 'Promise'.
../../../../../../../usr/local/lib/node_modules/typescript/lib/lib.es6.d.ts(5737,11): error TS2300: Duplicate identifier 'Promise'.

删除库,现在服务器将编译

➜  server git:(402-compile-errors) ✗ rm -rf ../node_modules/@types/es6-promise 
➜  server git:(402-compile-errors) ✗ tsc                                      
➜  server git:(402-compile-errors) ✗ cd ../client 

但现在客户端失败了

➜  client git:(402-compile-errors) ✗ tsc
src/components/GeneralChatAdminInputArea.tsx(100,14): error TS2304: Cannot find name 'Promise'.
src/components/GeneralChatAdminInputArea.tsx(103,16): error TS2304: Cannot find name 'Promise'.
src/routes/users/index.ts(11,21): error TS2304: Cannot find name 'Promise'.
src/routes/users/index.ts(12,21): error TS2304: Cannot find name 'Promise'.
src/routes/users/index.ts(13,3): error TS2304: Cannot find name 'Promise'.
➜  client git:(402-compile-errors) ✗ 

所以它看起来像一个 catch22

【问题讨论】:

  • 工会的回答对你有用吗?您必须在哪里添加该 lib 选项?

标签: javascript typescript promise typescript-typings


【解决方案1】:

您的tsconfig.json 中缺少lib:

// tsconfig.json
{
  "compilerOptions": {
    "lib": [
      "es2015"
    ]
  }
}

【讨论】:

  • 即使我的目标是 es6?这到底是从哪里来的?
  • 我也有单独的客户端和服务器的 tsconfig,那个咒语应该去哪里?
  • 我试过这个并得到error TS5023: Unknown compiler option 'lib'. 这是tsc 1.5.3
  • 更新您的 tsc。现在是 2.2
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-10
  • 2018-12-19
  • 1970-01-01
  • 2016-12-31
  • 2019-08-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多