简体   繁体   中英

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

I get this compile error:

property 'prototype' does not exist on value of type 'Base'

on the following class, how can I get typescript to recognise the prototype object as a type of native Object from a 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 is probably not what you mean, since instances of Base don't have a prototype property (see yourself at runtime). Base , however, does:

interface IBase {
  extend: any;
}

class Base implements IBase {
  constructor() {}

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

Of course, at this point, extend might as well be static, since it applies to all Base instances. Did you mean this instead?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM