Typescript-Generic-Type扩展并具有默认值

时间:2019-11-07 09:29:41

标签: reactjs typescript

我目前正在研究一些反应性的东西,我正在尝试做这样的事情:

class TextBase<P = {} extends TextBaseProps> extends React.PureComponent<P>

现在,这给了我一个错误:

Parsing error: '?' expected. (eslint)

我摆弄了一下,发现这并没有给我一个错误:

<P extends TextBaseProps = {}>

但是现在当我尝试使用该组件时,它会告诉我:

Property 'onPress' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<TextNormal> & Readonly<{}> & Readonly<{ children?: ReactNode; }>'

感谢您的帮助:)

编辑:

几乎忘记了界面:

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

重要 实际上,我完全忘记了重要的信息。我再次扩展了现在缺少属性的元素。类定义:

class TextNormal extends TextBase {
    public constructor(props: unknown) {
        super(props, {
            color: Colors.fg.hex(),
        });
    }
}

0 个答案:

没有答案