Ошибка свойства не существует в методе TypeScript stati c - PullRequest
0 голосов
/ 10 июля 2020

Ниже мой код TypeScript. В методе stati c возникает ошибка:

Свойство 'name' не существует для типа 'typeof Person'.

В чем причина эту ошибку и как ее исправить?

class Person {
    name: string = 'no name'
    constructor(protected id: string,){
    }
    showId=():string => {
        return this.id
    }
    static showname(){return this.name}
}

Ответы [ 2 ]

1 голос
/ 10 июля 2020
class Person {
    static name: string = 'no name'
    constructor(protected id: string,){
    }
    showId=():string => {
        return this.id
    }
    static showname(){return this.name}
}

или

class Person {
    name: string = 'no name'
    constructor(protected id: string,){
    }
    showId=():string => {
        return this.id
    }
    static showname(person: Person){return person.name}
}
0 голосов
/ 10 июля 2020

Вы не можете получить доступ к членам класса в контексте c stati. Свойство name также должно быть stati c.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...