Typescript with React - use HOC on a generic component class

后端 未结 5 2017
别那么骄傲
别那么骄傲 2020-12-30 05:14

I have a generic React component, say like this one:

class Foo extends React.Component, FooState> {
    constructor(props: F         


        
5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-30 05:45

    EDIT: After some changes to your code, it was only a wrong constraint T in your withTd function.

    // I needed to change the constraint on T, but you may adapt with your own needs
    export const withTd = >(
      TableElement: React.ComponentType
    ): React.SFC => (props: T) => (
      
        
      
    )
    
    // Explicitly typed constructor
    // Removed after EDIT
    //const FooW = Foo as new (props: FooProps) => Foo
    
    // Inferred as React.StatelessComponent>
    const FooWithTd = withTd(Foo)
    

    No longer relevant after EDIT :

    You may find more information at this issue https://github.com/Microsoft/TypeScript/issues/3960

提交回复
热议问题