reactjs - Tip Generic Typescript care se extinde și are implicit
În prezent lucrez la niște lucruri cu React și încercam să fac ceva de genul acesta:
class TextBase<P = {} extends TextBaseProps> extends React.PureComponent<P>
Acum aceasta îmi dă o eroare care este:
Parsing error: '?' expected. (eslint)
Am tot meșterit puțin cu el și am descoperit că asta nu îmi dă o eroare:
<P extends TextBaseProps = {}>
Dar acum, când încerc să folosesc acel component, îmi spune:
Property 'onPress' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<TextNormal> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'
Ajutorul este apreciat :)
EDITARE:
aproape că am uitat interfața:
interface TextBaseProps {
children?: string | string[];
onPress?: (event: GestureResponderEvent) => void;
}
tsc --noEmit --skipLibCheck
error TS2322: Type '{ children: string[]; onPress: () => void; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<TextNormal> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'.
Property 'onPress' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<TextNormal> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'.
38 <STD.TextNormal
IMPORTANT De fapt, am uitat complet o informație importantă. Extind din nou acel element pentru care proprietatea lipsește acum. Definiția clasei:
class TextNormal extends TextBase {
public constructor(props: unknown) {
super(props, {
color: Colors.fg.hex(),
});
}
}