TypeScript找不到姓名'Promise'智能感知错误

TypeScript找不到姓名'Promise'智能感知错误

问题描述:

我在TypeScript 2.1.4,Visual Studio 2015 Update 3中发现红色波浪型智能感知错误找不到姓名'Promise',例如下面的代码显示错误Promise:TypeScript找不到姓名'Promise'智能感知错误

/// <reference path="../typings/index.d.ts" /> 
import 'fetch'; 
import {HttpClient, json} from 'aurelia-fetch-client'; 
import {inject} from 'aurelia-framework'; 
import {BearerToken} from './common/bearer-token'; 

export class ApiToken 
{ 
.... 
    public getTokenSimplified(): Promise<BearerToken> 
    { 
     let tokenResult: BearerToken; 

     let p = new Promise<BearerToken>(function (resolve, reject) 
     { 
      // my code ommited 
     }); 
     return p; 
    } 
.... 
} 

TypeScript的编译没有错误,所以我可以通过这个,但我想找到一个解决方案。有谁知道如何解决这个问题?已经研究的StackOverflow和Github上我曾尝试以下:

  • 故宫从“ES6-承诺”安装ES6-承诺--save和进口{}无极添加到源文件的顶部

    这确实会导致红色波浪消失,但会导致构建错误“类型Promise不可分配以键入Promise。存在两种不同类型的名称,但它们不相关。”

    安装和引用npm的ts-promise会产生相同的“存在这个名称的两种不同类型”错误。

  • 分型安装DT〜ES6-垫片--save --global

    这会导致重复定义,例如重复在lib.es2015.core.d.ts标识符 'PROPERTYKEY'

  • 分型安装DT〜ES6-承诺--save --global

    这会导致错误重复标识符 '无极' 在lib.es2015。 iterable.d.ts

  • 分型安装蓝鸟--source NPM --save

    这失败编译时错误“类型无极是不能分配给键入‘蓝鸟’”,因为HttpClient的返回JavaScript诺言,不蓝鸟承诺。

  • NPM安装ES6-垫片--save和NPM安装@类型/ ES6-垫片--save-dev的

    这会导致重复定义,例如重复标识符 'PROPERTYKEY' 在lib.es2015.core.d.ts

  • NPM安装ES6-承诺--save和NPM安装@类型/ ES6-承诺--save-dev的

    原因错误重复标识符在lib.es2015.iterable.d.ts中的'Promise'

  • in tsconfig.json,将“lib”:[“es2015”,“dom”]修改为“lib”:[“es2015”,“es2015。承诺“,”dom“]没有解决问题。

tscconfig.json如下:

{ 
    "compileOnSave": false, 
    "compilerOptions": { 
    "rootDir": "src", 
    "outDir": "dist", 
    "sourceMap": true, 
    "target": "es5", 
    "module": "amd", 
    "declaration": false, 
    "noImplicitAny": false, 
    "removeComments": true, 
    "emitDecoratorMetadata": true, 
    "experimentalDecorators": true, 
    "moduleResolution": "node", 
    "lib": ["es2015", "dom"], 
    "baseUrl": "./", 
    "paths": { 
     "src/*": ["src/*"] 
    } 
    }, 
    "filesGlob": [ 
    "./src/**/*.ts", 
    "./test/**/*.ts", 
    "./typings/index.d.ts", 
    "./custom_typings/**/*.d.ts", 
    "./jspm_packages/**/*.d.ts" 
    ], 
    "exclude": [ 
    "node_modules", 
    "jspm_packages", 
    "dist", 
    "build", 
    "test" 

    ], 
    "atom": { 
    "rewriteTsconfig": false 
    } 
} 

也许我没有正确地引用所需的库,所以如果有人能指出错误我将不胜感激。

试试这个配置对于​​库

"lib": ["es2015", "dom", "es6"] 

如果其他类型的缺失(RequestResponseBufferSourceURLSearchParams ...)请发送您的typings.json文件。

+0

将“es6”添加到tsconfig.json没有区别。没有其他缺失的类型。 – Alastair

+0

@Alastair Promise 在lib.es2015.iterable.d.ts中定义。 标准安装路径:C:\ Program Files(x86)\ Microsoft Visual Studio 14.0 \ Common7 \ IDE \ CommonExtensions \ Microsoft \ TypeScript \ lib。 es2015.iterable.d.ts 是否安装了TypeScript 2.1.4以上的其他版本? 您是否通过Aurelia-CLI设置了您的Aurelia项目? – Myrddhin

+0

关于TypeScript版本:我使用的是PATH中指定的2.1.4以及项目的TypeScriptToolsVersion元素。当我安装TypeScript 2.1.4时,它安装在C:\ Program Files(x86)\ Microsoft SDKs \ TypeScript \ 2.1中,但是您提到的文件也存在。 WinMerge将两个文件显示为几乎相同(Promise和所有其他定义相同),尽管Microsoft SDK中的版本还包含一个ReadonlyArray接口定义。 – Alastair