Typescript 使用更严格的泛型重写方法
在typescript中使用d3.js,我试图用一个返回更具体的SVGRectElement选择的方法覆盖一个返回SVGElement选择的方法Typescript 使用更严格的泛型重写方法,typescript,generics,inheritance,d3.js,Typescript,Generics,Inheritance,D3.js,在typescript中使用d3.js,我试图用一个返回更具体的SVGRectElement选择的方法覆盖一个返回SVGElement选择的方法 class ElementSelector { public getSelection(): d3.Selection<SVGElement, any, any, any> { return d3.select<SVGElement, any>(""); } } class RectSelect
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>("");
}
}
类元素选择器{
public getSelection():d3.Selection{
返回d3,选择(“”);
}
}
类RectSelector扩展了ElementSelector{
public getSelection():d3.Selection{
返回d3,选择(“”);
}
}
但我得到了这个错误:
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'.
类“B”错误地扩展了基类“A”。
属性“getSelection”的类型不兼容。
类型“()=>Selection”不可分配给类型“()=>Selection”。
类型“Selection”不可分配给类型“Selection”。
属性“基准”的类型不兼容。
类型“{():any;(值:null):选择;(值:值…”不可分配给类型“{():any;(值:null):选择;(值:ValueFn
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 };
}
}