React/TypeScript: extending a component with additional properties

前端 未结 5 603
梦如初夏
梦如初夏 2021-02-01 16:05

I am trying to use react to recreate my currents components (written in pure typescript) but I can\'t find a way to give additional props to a component extending an other.

5条回答
  •  难免孤独
    2021-02-01 16:23

    The most elegant solution that I found (without extra generic class) is

    interface IBaseProps {
        name: string;
    }
    
    class Base

    extends React.Component

    { } interface IChildProps extends IBaseProps { id: number; } class Child extends Base { render(): JSX.Element { return (

    {this.props.id} {this.props.name}
    ); } }

提交回复
热议问题