Let's say I have a class Person
which looks like this:
class Person {
constructor(
public firstName: string,
public lastName: string,
public age: number
) {}
}
I have overridden the toString
method as follows.
public toString(): string {
return this.firstName + ' ' + this.lastName;
}
Now I expect to be able to do the following, which works in runtime:
function alertMessage(message: string) {
alert(message);
}
alertMessage(new Person('John', 'Smith', 20));
But this still gives me this error:
TS2345: Argument of type 'Person' is not assignable to parameter of type 'string'.
How can I assign a Person
to this string
argument?