Reputation: 32934
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
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