TypeScript 覆盖 ToString()
TypeScript override ToString()
假设我有一个 class Person
看起来像这样:
class Person {
constructor(
public firstName: string,
public lastName: string,
public age: number
) {}
}
我已经重写了 toString
方法,如下所示。
public toString(): string {
return this.firstName + ' ' + this.lastName;
}
现在我希望能够在运行时执行以下操作:
function alertMessage(message: string) {
alert(message);
}
alertMessage(new Person('John', 'Smith', 20));
但这仍然给我这个错误:
TS2345: Argument of type 'Person' is not assignable to parameter of type 'string'.
如何将 Person
分配给此 string
参数?
覆盖 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)
虽然有时可能会出现问题,但无法访问父 class 的 toString
:
console.log("" + (new Bar() as Foo));
将 运行 toString 放在 Bar 上,而不是放在 Foo 上。
正如@Kruga 所指出的,该示例实际上似乎在运行时有效JavaScript。唯一的问题是 TypeScript shows a type error。
TS2345: Argument of type 'Person' is not assignable to parameter of type 'string'.
要解决此消息,您必须:
- 显式调用
.toString()
- 或将对象与字符串连接(例如
`${obj}`
或 obj + ''
)
- 或使用
obj as any
(不推荐,因为你会失去类型安全)
假设我有一个 class Person
看起来像这样:
class Person {
constructor(
public firstName: string,
public lastName: string,
public age: number
) {}
}
我已经重写了 toString
方法,如下所示。
public toString(): string {
return this.firstName + ' ' + this.lastName;
}
现在我希望能够在运行时执行以下操作:
function alertMessage(message: string) {
alert(message);
}
alertMessage(new Person('John', 'Smith', 20));
但这仍然给我这个错误:
TS2345: Argument of type 'Person' is not assignable to parameter of type 'string'.
如何将 Person
分配给此 string
参数?
覆盖 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)
虽然有时可能会出现问题,但无法访问父 class 的 toString
:
console.log("" + (new Bar() as Foo));
将 运行 toString 放在 Bar 上,而不是放在 Foo 上。
正如@Kruga 所指出的,该示例实际上似乎在运行时有效JavaScript。唯一的问题是 TypeScript shows a type error。
TS2345: Argument of type 'Person' is not assignable to parameter of type 'string'.
要解决此消息,您必须:
- 显式调用
.toString()
- 或将对象与字符串连接(例如
`${obj}`
或obj + ''
) - 或使用
obj as any
(不推荐,因为你会失去类型安全)