【问题标题】:TypeScript - rest operator behaviour not working when targeting es2018TypeScript - 以 es2018 为目标时,其余运算符行为不起作用
【发布时间】:2018-02-28 21:59:32
【问题描述】:

此代码适用于 TypeScript Playground...

class Foo {
    constructor(...args: any[]) {

    }

    static make(...args: any[]): Foo {
        return new Foo(...args);
    }
}

Example

...但是当它在 Visual Studio 的 TypeScript 项目中时它不起作用。我在声明return new Foo(...args); 中收到args 的以下错误

类型必须有一个“Symbol.iterator”方法,该方法返回一个 迭代器。

这里有什么?

在本地机器上运行 TypeScript 2.7。当我将构建目标更改为 es2018

时出现问题

【问题讨论】:

  • 你的浏览器支持es2018吗?

标签: typescript


【解决方案1】:

这似乎是编译器中的一个错误,es2018 的默认库不正确。来自撰写本文时的编译器代码:

export function getDefaultLibFileName(options: CompilerOptions): string {
    switch (options.target) {
        case ScriptTarget.ESNext:
            return "lib.esnext.full.d.ts";
        case ScriptTarget.ES2017:
            return "lib.es2017.full.d.ts";
        case ScriptTarget.ES2016:
            return "lib.es2016.full.d.ts";
        case ScriptTarget.ES2015:
            return "lib.es6.d.ts";  // We don't use lib.es2015.full.d.ts due to breaking change.
        default:
            return "lib.d.ts";
    }
}

缺少 es2018 的选项。您可以手动指定适当的库:

{
    "compilerOptions": {
        "target": "es2018",
        "lib": [
            "es2018",
            "dom"
        ]
    }
}

【讨论】:

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