npm – Angular 2找不到Promise,Map,Set和Iterator

前端之家收集整理的这篇文章主要介绍了npm – Angular 2找不到Promise,Map,Set和Iterator前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
安装角度2后,我的typescript编译器不断得到一些错误,找不到Promise,Map,Set和Iterator。
直到现在我忽略了他们,但现在我需要Promise,所以我的代码将工作。
import {Component} from 'angular2/core';
@Component({
    selector: 'greeting-cmp',template: `<div>{{ asyncGreeting | async}}</div>`
})
export class GreetingCmp {
    asyncGreeting: Promise<string> = new Promise(resolve => {
// after 1 second,the promise will resolve
        window.setTimeout(() => resolve('hello'),1000);
    });
}

Additional information:
npm -v is 2.14.12
node -v is v4.3.1
typescript v is 1.6

错误

................ERROS OF MY CODE.................
    C:\Users\armyTik\Desktop\angular2\greeting_cmp.ts
    Error:(7,20) TS2304: Cannot find name 'Promise'.
    Error:(7,42) TS2304: Cannot find name 'Promise'.
    .........................................
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\platform\browser.d.ts
    Error:(77,90) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\core\application_ref.d.ts
    Error:(83,60) TS2304: Cannot find name 'Promise'.
    Error:(83,146) TS2304: Cannot find name 'Promise'.
    Error:(96,51) TS2304: Cannot find name 'Promise'.
    Error:(96,147) TS2304: Cannot find name 'Promise'.
    Error:(133,90) TS2304: Cannot find name 'Promise'.
    Error:(171,81) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\core\change_detection\parser\locals.d.ts
    Error:(3,14) TS2304: Cannot find name 'Map'.
    Error:(4,42) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\core\debug\debug_node.d.ts
    Error:(14,13) TS2304: Cannot find name 'Map'.
    Error:(24,17) TS2304: Cannot find name 'Map'.
    Error:(25,17) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\core\di\provider.d.ts
    Error:(436,103) TS2304: Cannot find name 'Map'.
    Error:(436,135) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\core\linker\compiler.d.ts
    Error:(12,50) TS2304: Cannot find name 'Promise'.
    Error:(16,41) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\core\linker\dynamic_component_loader.d.ts
    Error:(108,136) TS2304: Cannot find name 'Promise'.
    Error:(156,150) TS2304: Cannot find name 'Promise'.
    Error:(197,128) TS2304: Cannot find name 'Promise'.
    Error:(203,127) TS2304: Cannot find name 'Promise'.
    Error:(204,141) TS2304: Cannot find name 'Promise'.
    Error:(205,119) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\core\render\api.d.ts
    Error:(13,13) TS2304: Cannot find name 'Map'.
    Error:(14,84) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\facade\async.d.ts
    Error:(27,33) TS2304: Cannot find name 'Promise'.
    Error:(28,45) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\facade\collection.d.ts
    Error:(1,25) TS2304: Cannot find name 'MapConstructor'.
    Error:(2,25) TS2304: Cannot find name 'SetConstructor'.
    Error:(4,27) TS2304: Cannot find name 'Map'.
    Error:(4,39) TS2304: Cannot find name 'Map'.
    Error:(7,9) TS2304: Cannot find name 'Map'.
    Error:(8,30) TS2304: Cannot find name 'Map'.
    Error:(11,43) TS2304: Cannot find name 'Map'.
    Error:(12,27) TS2304: Cannot find name 'Map'.
    Error:(14,23) TS2304: Cannot find name 'Map'.
    Error:(15,25) TS2304: Cannot find name 'Map'.
    Error:(95,41) TS2304: Cannot find name 'Set'.
    Error:(96,22) TS2304: Cannot find name 'Set'.
    Error:(97,25) TS2304: Cannot find name 'Set'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\facade\lang.d.ts
    Error:(13,17) TS2304: Cannot find name 'Map'.
    Error:(14,17) TS2304: Cannot find name 'Set'.
    Error:(78,59) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\facade\promise.d.ts
    Error:(2,14) TS2304: Cannot find name 'Promise'.
    Error:(7,32) TS2304: Cannot find name 'Promise'.
    Error:(8,38) TS2304: Cannot find name 'Promise'.
    Error:(9,35) TS2304: Cannot find name 'Promise'.
    Error:(9,93) TS2304: Cannot find name 'Promise'.
    Error:(10,34) TS2304: Cannot find name 'Promise'.
    Error:(11,32) TS2304: Cannot find name 'Promise'.
    Error:(11,149) TS2304: Cannot find name 'Promise'.
    Error:(12,43) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\http\headers.d.ts
    Error:(43,59) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\http\url_search_params.d.ts
    Error:(11,16) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\platform\browser\browser_adapter.d.ts
    Error:(75,33) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\angular2\src\platform\dom\dom_adapter.d.ts
    Error:(85,42) TS2304: Cannot find name 'Map'.
    C:\Users\armyTik\Desktop\angular2\node_modules\rxjs\CoreOperators.d.ts
    Error:(35,67) TS2304: Cannot find name 'Promise'.
    Error:(50,66) TS2304: Cannot find name 'Promise'.
    Error:(89,67) TS2304: Cannot find name 'Promise'.
    Error:(94,38) TS2304: Cannot find name 'Promise'.
    Error:(94,50) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\rxjs\Observable.d.ts
    Error:(46,62) TS2304: Cannot find name 'Promise'.
    Error:(47,42) TS2304: Cannot find name 'Iterator'.
    Error:(103,74) TS2304: Cannot find name 'Promise'.
    Error:(103,84) TS2304: Cannot find name 'Promise'.
    Error:(143,66) TS2304: Cannot find name 'Promise'.
    Error:(158,65) TS2304: Cannot find name 'Promise'.
    Error:(201,66) TS2304: Cannot find name 'Promise'.
    Error:(206,38) TS2304: Cannot find name 'Promise'.
    Error:(206,50) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\rxjs\observable\ForkJoinObservable.d.ts
    Error:(6,50) TS2304: Cannot find name 'Promise'.
    Error:(7,58) TS2304: Cannot find name 'Promise'.
    C:\Users\armyTik\Desktop\angular2\node_modules\rxjs\observable\FromObservable.d.ts
    Error:(7,38) TS2304: Cannot find name 'Promise'.
    Error:(7,51) TS2304: Cannot find name 'Iterator'.
    C:\Users\armyTik\Desktop\angular2\node_modules\rxjs\observable\PromiSEObservable.d.ts
    Error:(9,31) TS2304: Cannot find name 'Promise'.
    Error:(10,26) TS2304: Cannot find name 'Promise'.
ANGULAR RC4 – Angular 2.0.0 final with Typescript 2.0.0

更新于2016年9月19日

为了使这个工作与typescript 2.0.0,我做了以下。

npm install –save-dev @ types / core-js

tsconfig.json

"compilerOptions": {
    "declaration": false,"emitDecoratorMetadata": true,"experimentalDecorators": true,"mapRoot": "./","module": "es6","moduleResolution": "node","noEmitOnError": true,"noImplicitAny": false,"outDir": "../dist/out-tsc","sourceMap": true,"target": "es5","typeRoots": [
      "../node_modules/@types"
    ],"types": [
      "core-js"
    ]
  }

更多关于@types与typescript 2.0.0。

> https://blogs.msdn.microsoft.com/typescript/2016/06/15/the-future-of-declaration-files/
> https://www.npmjs.com/~types

安装示例:

npm install --save-dev @types/core-js

重复的标识符错误

这很可能是因为重复的ecmascript 6类型已经从其他地方导入,最有可能是旧的es6-shim。

双重检查typings.d.ts确保没有引用es6。从你的typings目录中删除对es6的任何引用(如果有的话)。

例如:

这将与类型:[‘core-js’]在typings.json中冲突。

{
  "globalDependencies": {
    "core-js": "registry:dt/core-js#0.0.0+20160602141332" 
    // es6-shim will also conflict
  }
}

在tsconfig.json中的types数组中包含core-js应该是从其中导入的唯一位置。

Angular CLI 1.0.0-beta.11-webpack

如果您使用Angular-CLI,请删除typings.json中的lib数组。这似乎与在类型中声明core-js冲突。

"compilerOptions" : {
  ...
  // removed "lib": ["es6",dom"],...
},"types" : ["core-js"]

Webstorm / Intellij用户使用Angular CLI

确保内置的typescript编译器被禁用。这将与CLI冲突。要使用CLI编译脚本,您可以设置ng服务配置。

Tsconfig compilerOptions lib vs types

如果你不想安装核心js类型定义,有一些es6库包含在typescript中。这些通过tsconfig中的lib:[]属性使用。

参见这里例如:https://www.typescriptlang.org/docs/handbook/compiler-options.html

Note: If –lib is not specified a default library is injected. The
default library injected is: ► For –target ES5: DOM,ES5,ScriptHost ►
For –target ES6: DOM,ES6,DOM.Iterable,ScriptHost

tl; dr

简短回答“lib”:[“es6”,“dom”]或“types”:[“core-js”]可以用来解决找不到Promise,Map,Set和Iterator。然而,使用两者将导致重复的标识符错误

猜你在找的Angularjs相关文章