在typescript中使用d3.js我试图覆盖一个返回SVGElement选择的方法,该方法返回一个更具体的SVGRectElement选择。
class ElementSelector {
public getSelection(): d3.Selection<SVGElement, any, any, any> {
return d3.select<SVGElement, any>("");
}
}
class RectSelector extends ElementSelector {
public getSelection(): d3.Selection<SVGRectElement, any, any, any> {
return d3.select<SVGRectElement, any>("");
}
}
但是我收到了这个错误:
Class 'B' incorrectly extends base class 'A'.
Types of property 'getSelection' are incompatible.
Type '() => Selection<SVGRectElement, any, any, any>' is not assignable to type '() => Selection<SVGElement, any, any, any>'.
Type 'Selection<SVGRectElement, any, any, any>' is not assignable to type 'Selection<SVGElement, any, any, any>'.
Types of property 'datum' are incompatible.
Type '{ (): any; (value: null): Selection<SVGRectElement, undefined, any, any>; <NewDatum>(value: Value...' is not assignable to type '{ (): any; (value: null): Selection<SVGElement, undefined, any, any>; <NewDatum>(value: ValueFn<S...'.
Type 'Selection<SVGRectElement, undefined, any, any>' is not assignable to type 'Selection<SVGElement, undefined, any, any>'.
Types of property 'on' are incompatible.
Type '{ (typenames: string): ValueFn<SVGRectElement, undefined, void> | undefined; (typenames: string, ...' is not assignable to type '{ (typenames: string): ValueFn<SVGElement, undefined, void> | undefined; (typenames: string, list...'.
Type 'ValueFn<SVGRectElement, undefined, void> | undefined' is not assignable to type 'ValueFn<SVGElement, undefined, void> | undefined'.
Type 'ValueFn<SVGRectElement, undefined, void>' is not assignable to type 'ValueFn<SVGElement, undefined, void> | undefined'.
Type 'ValueFn<SVGRectElement, undefined, void>' is not assignable to type 'ValueFn<SVGElement, undefined, void>'.
The 'this' types of each signature are incompatible.
Type 'SVGElement' is not assignable to type 'SVGRectElement'.
显然我无法将SVGRectElement分配给SVGElement,但原因并不清楚。
例如,这有效:
interface Gen<T> {
prop: T
}
class A {
init(param1: number): Gen<SVGElement> {
return { prop: {} as SVGElement };
}
}
class B extends A {
init(param1: number): Gen<SVGRectElement> {
return { prop: {} as SVGRectElement };
}
}
我错过了什么?