【问题标题】:TypeScript Cannot find name 'Promise' intellisense errorTypeScript 找不到名称“Promise”智能感知错误
【发布时间】: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


    【解决方案1】:

    试试这个库配置

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

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

    【讨论】:

    • 将“es6”添加到 tsconfig.json 没有任何区别。没有其他缺失的类型。
    • @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 是否安装了 2.1.4 以外的其他 TypeScript 版本?您是否通过 Aurelia-CLI 设置了您的 Aurelia 项目?
    • 关于 TypeScript 版本:我使用的是 PATH 和项目的 TypeScriptToolsVersion 元素中指定的 2.1.4。 Wnen 我安装了 TypeScript 2.1.4,它安装在 C:\Program Files (x86)\Microsoft SDKs\TypeScript\2.1 中,但是你提到的文件也存在。 WinMerge 显示这两个文件实际上是相同的(Promise 和所有其他定义都相同),尽管 Microsoft SDK 中的版本还包括一个 ReadonlyArray 接口定义。
    • 我没有使用 Aurelia-CLI 来设置项目,因为它不再被推荐。我尝试了以下但智能感知错误仍然存​​在:“lib”:[“es2015”,“dom”,“es2015.iterable”] 以及“lib”:[“es2015”,“dom”,“es2015.promise”]
    • 我通过将 Aurelia Typescript 骨架 v1.1.2 复制到 ASP.NET MVC 项目的根目录中来设置项目。
    猜你喜欢
    • 2022-07-04
    • 1970-01-01
    • 1970-01-01
    • 2016-01-24
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多