[英]Typescript - Property does not exist on type when attempting to extend a Class prototype
I'm using Typescript and FabricJS, and I'm attempting to extend the 'Point' class. 我正在使用Typescript和FabricJS,我正在尝试扩展'Point'类。 Here's what it looks like: 这是它的样子:
export class Point {
x: number;
y: number;
constructor(x: number, y: number);
add(that: Point): Point;
addEquals(that: Point): Point;
// ...(more methods)
}
Here's my attempt to extend it and add a method, in another file: 这是我尝试扩展它并在另一个文件中添加一个方法:
import { Point } from 'fabric/fabric-impl';
export interface Point {
a(): any;
}
Point.prototype.a = function () { }; // line that gives error
Here I get the error "[ts] Property 'a' does not exist on type 'Point'. [2339]".
在这里我得到错误"[ts] Property 'a' does not exist on type 'Point'. [2339]".
I'm able to get something similar to this working using Typescript's 'extends', by creating a subclass: 通过创建子类,我可以使用Typescript的'extends'来获得类似的东西:
interface myPoint {
a: any;
}
class myPoint extends Point {
constructor(x: number, y: number) {
super(x, y);
}
}
myPoint.prototype.a = function () { };
This works fine, but I'd rather add directly the method directly to the Point class. 这很好,但我宁愿直接将方法直接添加到Point类。 Any ideas on what's wrong? 关于什么是错的任何想法?
You need to place the Point
interface inside a module declaration. 您需要将Point
接口放在模块声明中。 This will extend the original type inside the original module instead of declaring a new type: 这将扩展原始模块中的原始类型,而不是声明一个新类型:
import { Point } from 'fabric/fabric-impl';
declare module 'fabric/fabric-impl' {
export interface Point {
a(): any;
}
}
Point.prototype.a = function () { }; // ok now
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.