Rayden C
Rayden C

Reputation: 149

Property does not exist error in TypeScript static method

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

Answers (2)

dm.shpak
dm.shpak

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

Kai Schneider
Kai Schneider

Reputation: 1474

You cannot access class members in a static context. The name property also needs to be static.

Upvotes: 3

Related Questions