使用保留关键字作为Typescript中类/实例的方法

mus*_*_ut 3 javascript internet-explorer-8 typescript

如果我在Typescript类中使用保留关键字,如deletecontinue方法名称,那么我遇到了IE8的问题.

例如:

class Foo {
    delete() : void {
        console.log('Delete called');
    }

    continue() : void {
        console.log('Continue called');
    }
}
Run Code Online (Sandbox Code Playgroud)

生成以下javascript:

var Foo = (function () {
    function Foo() {
    }
    Foo.prototype.delete = function () {
        console.log('Delete called');
    };

    Foo.prototype.continue = function () {
        console.log('Continue called');
    };
    return Foo;
})();
Run Code Online (Sandbox Code Playgroud)

但是,这与IE8并不顺利.IE8宁愿这样做:

var Bar = (function () {
    function Bar() {
    }
    Bar.prototype['delete'] = function () {
        console.log('Delete called');
    };

    Bar.prototype['continue'] = function () {
        console.log('Continue called');
    };
    return Bar;
})();
Run Code Online (Sandbox Code Playgroud)

有没有一种方法可以保持Typescript的Typed良好,同时保持我的代码IE8表现良好?

在我的特定情况下,我试图编写一个实现ng.IHttpService接口的类,因此需要delete作为实例方法.

Blu*_*ies 6

class Foo {
    "delete"() : void {
        console.log('Delete called');
    }

    "continue"() : void {
        console.log('Continue called');
    }
}
Run Code Online (Sandbox Code Playgroud)

翻译成

var Foo = (function () {
    function Foo() {
    }
    Foo.prototype["delete"] = function () {
        console.log('Delete called');
    };

    Foo.prototype["continue"] = function () {
        console.log('Continue called');
    };
    return Foo;
})();
Run Code Online (Sandbox Code Playgroud)

请注意,我从未使用过TypeScript.我刚刚访问他们的网站,快速浏览规范PDF,发现可以使用字符串文字,并在TypeScript操场上尝试过.