简体   繁体   English

Typescript:如何在构造函数上声明原型对象的类型

[英]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). this.prototype可能不是您的意思,因为Base实例没有prototype属性(请在运行时查看)。 Base , however, does: 但是, Base可以:

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. 当然,在这一点上, extend可能也是静态的,因为它适用于所有Base实例。 Did you mean this instead? 你是说这个吗?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何声明返回自己原型的 TypeScript 类型的函数? - How to declare TypeScript type of function that returns own prototype? 在Typescript中,我如何声明一个返回字符串类型数组的函数? - In Typescript how do I declare a function that return string type array? `如何构造构造函数 <T> = Function&{prototype:T}`是否适用于TypeScript中的Abstract构造函数类型? - How does `type Constructor<T> = Function & { prototype: T }` apply to Abstract constructor types in TypeScript? 如何在返回与 object 实例相同类型的 Obect 原型上声明 typescript 属性 - How to declare a typescript property on Obect prototype that returns the same type as the object instance 如何在打字稿中声明函数类型 - How to declare a function type in typescript typescript:如何声明 function rturn type that inside object - typescript: how to declare function rturn type that inside of object 如何在 TypeScript 中同时使用“Pick”和“&amp;”关键字声明数组 object 的类型 - How do I declare a type for array object using 'Pick' and '&' keyword at the same time in TypeScript 如何在 TypeScript 中推断 object function 参数的确切类型? - How do I infer the exact type of an object function parameter in TypeScript? 如何为 NPM package 原型 object 声明环境 typescript class? - How to declare ambient typescript class for an NPM package prototype object? 如何在TypeScript中的地图中声明值的类型? - How do I declare the type of a value in a map in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM