TypeScript workaround for rest props in React

前端 未结 6 779
迷失自我
迷失自我 2021-02-06 22:50

Updated for TypeScript 2.1

TypeScript 2.1 now supports object spread/rest, so no workarounds are needed anymore!


Original Question

TypeScript s

6条回答
  •  失恋的感觉
    2021-02-06 23:16

    It's actually easier than all of the answers above. You just need to follow the example below:

    type Props = {
      id: number,
      name: string;
      // All other props
      [x:string]: any;
    }
    
    const MyComponent:React.FC = props => {
      // Any property passed to the component will be accessible here
    }
    

    Hope this helps.

提交回复
热议问题