【发布时间】:2017-05-31 09:43:14
【问题描述】:
我在 TypeScript 2.1.4、Visual Studio 2015 更新 3 中收到一个红色波浪状智能感知错误,说 找不到名称“Promise”,例如以下代码显示了两种使用 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 之后,我尝试了以下方法:
-
npm install es6-promise --save, and import {Promise} from 'es6-promise' 添加到源文件的顶部
这确实会导致红色波浪消失,但会导致构建错误“类型 Promise 不可分配给类型 Promise。存在具有此名称的两种不同类型,但它们不相关。”
安装和引用 npm 的 ts-promise 会产生相同的“存在同名的两种不同类型”错误。
-
typings install dt~es6-shim --save --global
这会导致重复定义,例如lib.es2015.core.d.ts 中的重复标识符“PropertyKey”
-
typings install dt~es6-promise --save --global
这会导致 lib.es2015.iterable.d.ts 中出现重复标识符“Promise”错误
-
typings install bluebird --source npm --save
由于 HttpClient 返回 Javascript Promises,而不是 Bluebird 承诺,因此编译时错误“Type Promise is not assignable to type 'Bluebird'”失败。
-
npm install es6-shim --save 和 npm install @types/es6-shim --save-dev
这会导致重复定义,例如lib.es2015.core.d.ts 中的重复标识符“PropertyKey”
-
npm install es6-promise --save 和 npm install @types/es6-promise --save-dev
导致 lib.es2015.iterable.d.ts 中出现重复标识符“Promise”错误
在 tsconfig.json 中,将 "lib": ["es2015", "dom"] 修改为 "lib": ["es2015", "es2015.promise", "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
}
}
也许我没有正确引用所需的库,所以如果有人能指出错误,我将不胜感激。
【问题讨论】:
标签: typescript2.0