David Thielen
David Thielen

Reputation: 32934

Can I implement operator overrides in typescript?

Say I am creating a Dictionary class in typescript. Is there a way to have operator overrides so I can define the operator "[string]" instead of having to use the function get(string)?

Upvotes: 0

Views: 698

Answers (1)

Ryan Cavanaugh
Ryan Cavanaugh

Reputation: 220974

No, you cannot have operator overloading. JavaScript does not have a notion of this.

You can override toString, though:

class Thing {
    toString() {
        return 'I am a Thing!';
    }
}

var x = new Thing();
console.log('X says ' + x); // Prints "X says I am a Thing!"

Upvotes: 2

Related Questions