Reputation: 157
I am new to typescript(and OOP). I found the following example in their official documentation:
class Student {
fullName: string;
constructor(public firstName: string, public middleInitial: string, public lastName: string) {
this.fullName = firstName + " " + middleInitial + " " + lastName;
}
}
interface Person {
firstName: string;
lastName: string;
}
function greeter(person : Person) {
return "Hello, " + person.firstName + " " + person.lastName;
}
let user = new Student("Jane", "M.", "User");
document.body.innerHTML = greeter(user);
The way I see it, class Student
doesn't have the property lastname
and thus shouldn't be compatible with the interface Person
when the function greeter
is being called.
What am I missing here?
Upvotes: 0
Views: 46
Reputation: 250086
The class does have a field lastName
. public lastName: string
is a shorthand field declaration. This is both a public
field and a parameter declaration. That is the meaning of the modifier in the constructor parameter. See the docs for more information
Upvotes: 2