[英]Property does not exist error in TypeScript static method
Below is my TypeScript code.下面是我的 TypeScript 代码。 In the static method it is throwing an error:
在 static 方法中它抛出一个错误:
Property 'name' does not exist on type 'typeof Person'.
“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.您不能在 static 上下文中访问 class 成员。 The name property also needs to be static.
name 属性也需要是 static。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.