【问题标题】:How do I use Promises with TypeScript when targeting es5?以 es5 为目标时,如何将 Promises 与 TypeScript 一起使用?
【发布时间】:2019-08-27 11:17:38
【问题描述】:
Promise.all<any, any>(ajaxRequests).then(()=> {
    console.log("done");
});

以上代码给出以下编译错误:

TS2585: 'Promise' only refers to a type, but is being used as a value here. Do you need to change your target library? Try changing the `lib` compiler option to es2015 or later.

我不熟悉这个编译器 lib 选项是什么以及如果我要更改它会产生什么影响。


我正在尝试针对较旧的浏览器并且需要支持 es5 我相信。我认为这可以通过转译/polyfilling来完成?我的打字稿配置是:

{
    "compilerOptions": {
        "sourceMap": true,
        "target": "es5",
        "declaration": true,
        "removeComments": false,
        "module" : "commonjs",
        "moduleResolution": "node",
        "resolveJsonModule": true,
    },
    "include": [
        "src/*"
    ],

    "exclude": [
        "node_modules",
        "**/*.spec.ts"
    ]
}

【问题讨论】:

  • 你应该添加类似@types/es6-promise的东西

标签: typescript ecmascript-6 promise es5-compatiblity


【解决方案1】:

将以下内容添加到您的编译器选项中:

"lib": [
        "dom",
        "es5",
        "es2015.promise"
    ]

lib 选项在here 中有更详细的描述。

Here's 解释targetlib 之间的区别。

话虽如此,如果您可以接受使用es6,那么我认为您可以将target 设置为"es6",而不是乱用lib

【讨论】:

    猜你喜欢
    • 2021-10-28
    • 2017-07-22
    • 2015-08-21
    • 2017-11-13
    • 1970-01-01
    • 2017-02-21
    • 2017-12-22
    • 1970-01-01
    相关资源
    最近更新 更多