Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using reserved keywords as methods on Classes/Instances in Typescript

If I use reserved keywords such as delete and continue as method names in Typescript classes, then I run into a problem with IE8.

For example:

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

    continue() : void {
        console.log('Continue called');
    }
}

generates the following javascript:

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

    Foo.prototype.continue = function () {
        console.log('Continue called');
    };
    return Foo;
})();

However, this does not fly well with IE8. IE8 would prefer to have this instead:

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

    Bar.prototype['continue'] = function () {
        console.log('Continue called');
    };
    return Bar;
})();

Is there a way I can retain the Typed goodness of Typescript while keeping my code IE8-well behaved?

In my particular case, I am trying to write a class which implements the ng.IHttpService interface and, hence, needs to have delete as an instance method.

like image 671
musically_ut Avatar asked Dec 29 '25 23:12

musically_ut


1 Answers

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

    "continue"() : void {
        console.log('Continue called');
    }
}

translates to

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

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

Note that I've never used TypeScript. I just went to their site, took a quick look at the specification PDF, found that string literals can be used, and tried it in the TypeScript playground.

like image 68
Blue Skies Avatar answered Dec 31 '25 12:12

Blue Skies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!