Is there a way to overload a typescript getter / setter?
I know that typescript provides function overloading so i came up with something like this:
public get stringOrNumber(): string { return this._stringOrNumber; }
public set stringOrNumber(value: number);
public set stringOrNumber(value: string) {
if(typeof stringOrNumber == 'number') {
this._stringOrNumber = value.toString();
} else {
this._stringOrNumber = value;
}
}
But unfortunately this doesn't work =)
Do you mean something like this?
class A {
protected _stringOrNumber: string|number;
public get stringOrNumber(): string|number {
return this._stringOrNumber;
}
public set stringOrNumber(value: string|number) {
if(typeof this.stringOrNumber === 'number') {
this._stringOrNumber = value.toString();
} else {
this._stringOrNumber = value;
}
}
}
[Playground]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With