Typescript错误ts2416将prop添加到子类会导致类型无可分配错误

Typescript错误ts2416将prop添加到子类会导致类型无可分配错误,typescript,class,inheritance,subclass,Typescript,Class,Inheritance,Subclass,我正在将一些C#代码从一本书转换成TypeScript,我遇到了一个我不完全理解的问题,在这里或TypeScript文档中找不到答案,等等 我的代码定义了两个类,一个基类实体和一个子类参与者。Actor类从Entity继承“name”和“description”,两者都是字符串,并且它添加了自己的属性“location”,这是一个数字。然而TypeScript抱怨说位置应该是一个字符串。为什么? 代码如下: /** * base class */ export class Entity {

我正在将一些C#代码从一本书转换成TypeScript,我遇到了一个我不完全理解的问题,在这里或TypeScript文档中找不到答案,等等

我的代码定义了两个类,一个基类实体和一个子类参与者。Actor类从Entity继承“name”和“description”,两者都是字符串,并且它添加了自己的属性“location”,这是一个数字。然而TypeScript抱怨说位置应该是一个字符串。为什么?

代码如下:

/**
 * base class
 */
export class Entity {
  private _name: string;
  private _description: string;

  protected constructor(aName:string, aDescription:string) {
    this._name = aName;
    this._description = aDescription;
  }

  get name(): string {
    return this._name;
  }

  set location(newName: string) {
    this._name = newName;
  }

  get description(): string {
    return this._description;
  }

  set description(newDescription: string) {
    this._description = newDescription;
  }
}

/**
 * subclass
 */
export class Actor extends Entity{
  private _location:number;

  public constructor(aName:string, aDescription:string, aRoom:number) {
    super(aName, aDescription)
    this._location = aRoom;
  }

  get location(): number {
    return this._location;
  }

  set location(newRoom: number) {
    this._location = newRoom;
  }
}
下面是我得到的错误截图(使用VS代码):

我的错误


我在实体中错误地命名了setter。

因为您在
实体中错误地命名了
名称
setter
…糟糕。谢谢:facepalm: