在打字稿中,如何返回使用泛型的超类的子类?
这就是我希望的工作方式:
interface SuperProps {}
class SuperComponent<P extends SuperProps> extends React.Component<P> { }
interface SubProps extends SuperProps {}
class SubComponent extends SuperComponent<SubProps> { }
function get(): typeof SuperComponent {
return SubComponent;
}
但是,出现以下错误:
error TS2322: Type 'typeof SubComponent' is not assignable to type 'typeof SuperComponent'.
Type 'SubComponent' is not assignable to type 'SuperComponent<P>'.
Types of property 'props' are incompatible.
Type 'Readonly<{ children?: ReactNode; }> & Readonly<SubProps>' is not assignable to type 'Readonly<{ children?: ReactNode; }> & Readonly<P>'.
Type 'Readonly<{ children?: ReactNode; }> & Readonly<SubProps>' is not assignable to type 'Readonly<P>'.
这基本上是说SubProps!= P,对吧?我该怎么办?