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

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

I wrote some code:

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


        
相关标签:
13条回答
  • 2020-11-28 02:40

    The following worked for me: https://github.com/microsoft/TypeScript/issues/28631#issuecomment-472606019 I fix it by doing something like this:

    const Component = (isFoo ? FooComponent : BarComponent) as React.ElementType
    
    0 讨论(0)
  • 2020-11-28 02:44

    When declaring React Class component, use React.ComponentClass instead of React.Component then it will fix the ts error.

    0 讨论(0)
  • 2020-11-28 02:45

    If you really don't care about props then the widest possible type is React.ReactType.

    This would allow passing native dom elements as string. React.ReactType covers all of these:

    renderGreeting('button');
    renderGreeting(() => 'Hello, World!');
    renderGreeting(class Foo extends React.Component {
       render() {
          return 'Hello, World!'
       }
    });
    
    0 讨论(0)
  • 2020-11-28 02:45

    If you are using material-ui, go to type definition of the component, which is being underlined by TypeScript. Most likely you will see something like this

    export { default } from './ComponentName';
    

    You have 2 options to resolve the error:

    1.Add .default when using the component in JSX:

    import ComponentName from './ComponentName'
    
    const Component = () => <ComponentName.default />
    

    2.Rename the component, which is being exported as "default", when importing:

    import { default as ComponentName } from './ComponentName'
    
    const Component = () => <ComponentName />
    

    This way you don't need to specify .default every time you use the component.

    0 讨论(0)
  • 2020-11-28 02:47

    In my case I was missing new inside the type definition.

    some-js-component.d.ts file:

    import * as React from "react";
    
    export default class SomeJSXComponent extends React.Component<any, any> {
        new (props: any, context?: any)
    }
    

    and inside the tsx file where I was trying to import the untyped component:

    import SomeJSXComponent from 'some-js-component'
    
    const NewComp = ({ asdf }: NewProps) => <SomeJSXComponent withProps={asdf} />
    
    0 讨论(0)
  • 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<myProps, any, any>'.
    

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

    type ComponentType<P = {}> = ComponentClass<P, any> | FunctionComponent<P>
    
    0 讨论(0)
提交回复
热议问题