TypeScript workaround for rest props in React

前端 未结 6 770
迷失自我
迷失自我 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:15

    A getter like this could work:

    class Link extends React.Component<{
      textToDisplay: string;
    } & React.HTMLAttributes> {
    
      static propTypes = {
        textToDisplay: PropTypes.string;
      }
    
      private get HtmlProps(): React.HTMLAttributes {
        return Object.fromEntries(
          Object.entries(this.props)
          .filter(([key]) => !Object.keys(Link.propTypes).includes(key))
        );
      }
    
      public render():JSX.Element {
        return (
          
            {this.props.textToDisplay}
          
        );
      }
    }
    
    
    

提交回复
热议问题