Reputation: 149
Below is my TypeScript code. In the static method it is throwing an error:
Property 'name' does not exist on type 'typeof Person'.
What is the cause of this error and how do I fix it?
class Person {
name: string = 'no name'
constructor(protected id: string,){
}
showId=():string => {
return this.id
}
static showname(){return this.name}
}
Upvotes: 2
Views: 2025
Reputation: 355
class Person {
static name: string = 'no name'
constructor(protected id: string,){
}
showId=():string => {
return this.id
}
static showname(){return this.name}
}
or
class Person {
name: string = 'no name'
constructor(protected id: string,){
}
showId=():string => {
return this.id
}
static showname(person: Person){return person.name}
}
Upvotes: 2
Reputation: 1474
You cannot access class members in a static context. The name property also needs to be static.
Upvotes: 3