这段代码在typescript操场上运行…

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

    }

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

Example
…但是当它在visual studio中的typescript项目中时,它不起作用。我在语句args中得到return new Foo(...args);的以下错误
类型必须具有返回
迭代器。
上面是什么?
在本地计算机上运行TypeScript2.7。当我将构建目标更改为ES2018时会出现问题

最佳答案

这似乎是编译器中的一个错误,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"
        ]
    }
}

关于typescript - TypeScript-以es2018为目标时,剩余运算符(operator)行为不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49039308/

10-16 18:13