簡體   English   中英

Typescript:如何在構造函數上聲明原型對象的類型

[英]Typescript : How do I declare type of prototype Object on constructor Function

我收到此編譯錯誤:

屬性“原型”在類型“基礎”的值上不存在

在以下類上,如何從構造函數中獲取打字稿以將原型對象識別為本地對象的一種類型?

interface IBase {
  extend: any;
  prototype : any;
}

declare var Base : IBase;

class Base implements IBase {

  constructor() {}

  public extend( mixins : any ) : void {
    _.extend( this.prototype, mixins );
  }

}

this.prototype可能不是您的意思,因為Base實例沒有prototype屬性(請在運行時查看)。 但是, Base可以:

interface IBase {
  extend: any;
}

class Base implements IBase {
  constructor() {}

  public extend( mixins : any ) : void {
    _.extend(Base.prototype, mixins );
  }
}

當然,在這一點上, extend可能也是靜態的,因為它適用於所有Base實例。 你是說這個嗎?

  public extend( mixins : any ) : void {
    _.extend(this, mixins);
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM