简体   繁体   中英

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}
}
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}
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM