Ответ 1
Переопределение toString
работает, как и ожидалось:
class Foo {
private id: number = 23423;
public toString = () : string => {
return 'Foo (id: ${this.id})';
}
}
class Bar extends Foo {
private name:string = "Some name";
public toString = () : string => {
return 'Bar (${this.name})';
}
}
let a: Foo = new Foo();
// Calling log like this will not automatically invoke toString
console.log(a); // outputs: Foo { id: 23423, toString: [Function] }
// To string will be called when concatenating strings
console.log("" + a); // outputs: Foo (id: 23423)
console.log('${a}'); // outputs: Foo (id: 23423)
// and for overridden toString in subclass..
let b: Bar = new Bar();
console.log(b); // outputs: Bar { id: 23423, toString: [Function], name: 'Some name' }
console.log("" + b); // outputs: Bar (Some name)
console.log('${b}'); // outputs: Bar (Some name)
// This also works as expected; toString is run on Bar instance.
let c: Foo = new Bar();
console.log(c); // outputs: Bar { id: 23423, toString: [Function], name: 'Some name' }
console.log("" + c); // outputs: Bar (Some name)
console.log('${c}'); // outputs: Bar (Some name)
Иногда проблема может заключаться в том, что невозможно получить доступ к toString
родительского класса:
console.log("" + (new Bar() as Foo));
Запустит toString в баре, а не в Foo.