简体   繁体   English

TypeScript static 方法中的属性不存在错误

[英]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.

相关问题 Typescript static 方法在 someclass 类型上不存在 - Typescript static method does not exist on type someclass 尝试访问 TypeScript 中的静态方法时,类型“typeof UserApiController”上不存在属性“users_index” - Property 'users_index' does not exist on type 'typeof UserApiController' when trying to access static method in TypeScript 打字稿类型错误属性不存在 - Typescript type error property does not exist 打字稿:属性不存在 - Typescript: Property does not exist 类型{}不存在Typescript属性 - Typescript property does not exist on type {} 类型 [] 打字稿上不存在属性 - property does not exist on type [] typescript 属性在类型上不存在-TypeScript - Property does not exist on type - TypeScript TypeScript构建错误:“ IStateParamsService”类型不存在属性 - TypeScript Build Error : Property does not exist on type 'IStateParamsService' 打字稿错误:类型“元素”上不存在属性“包含”。 - Typescript error :Property 'contains' does not exist on type 'Element'.? "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM