musically_ut
musically_ut

Reputation: 34288

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.

Upvotes: 3

Views: 1609

Answers (1)

Blue Skies
Blue Skies

Reputation: 2975

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.

Upvotes: 6

Related Questions