What does the error “JSX element type '…' does not have any construct or call signatures” mean?

后端 未结 13 2309
余生分开走
余生分开走 2020-11-28 02:05

I wrote some code:

function renderGreeting(Elem: React.Component) {
    return Hello, !;
}
         


        
13条回答
  •  被撕碎了的回忆
    2020-11-28 02:52

    As @Jthorpe alluded to, ComponentClass only allows either Component or PureComponent but not a FunctionComponent.

    If you attempt to pass a FunctionComponent, typescript will throw an error similar to...

    Type '(props: myProps) => Element' provides no match for the signature 'new (props: myProps, context?: any): Component'.
    

    However, by using ComponentType rather than ComponentClass you allow for both cases. Per the react declaration file the type is defined as...

    type ComponentType

    = ComponentClass | FunctionComponent

提交回复
热议问题