Generic React components in TypeScript/JSX?

后端 未结 2 822
被撕碎了的回忆
被撕碎了的回忆 2021-02-19 07:23

I would like to create pluggable React components. Components are resolved by their class names, so I am naturally drawn to generics; but this doesn\'t seem to work.

<         


        
2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-19 08:06

    The accepted answer for this question still stands, due to TypeScript types being erased, however as of Typescript 2.9, generic JSX components are supported

    The example provided is:

    class GenericComponent

    extends React.Component

    { internalProp: P; } type Props = { a: number; b: string; }; const x = a={10} b="hi"/>; // OK const y = a={10} b={20} />; // Error

    Just thought it worth mentioning for anyone who ends up here via the question title.

提交回复
热议问题