javascript - Typescript-typningar - Duplicerat namn Promise/Kan inte hitta namnet Promise


Jag bygger en klient-/server-JS-app och har ett stort problem med att använda Promises. De är antingen odefinierade eller duplicerade och det verkar bero på @types-paketet.

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

detta kommer att ge serverfel som detta:

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) ✗ 

Om jag tar bort det paketet får jag klientfel:

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'.

Vad är vägen ut ur detta?

Min tsconfig.json ser ut så här:

{
    "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
    }
}

här är en annan version som visar problemet kommer och går

klienten kompilerar utan problem

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

men servern misslyckas

➜  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'.

ta bort biblioteket och nu kommer servern att kompilera

➜  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 

men nu misslyckas klienten

➜  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) ✗ 

Så det verkar som ett moment 22

Svar #1

Saknar lib i din tsconfig.json:

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