TypeScript: JSX element type does not have any construct or call signatures in v3.2.0-rc
TypeScript Version: 3.2.0-rc, 3.2.1
Code
import * as React from "react";
import { Component, ReactType} from "react";
function App(props:{component:ReactType}) {
const Comp: ReactType = props.component
return (<Comp />)
}
Expected behavior: Should output normal as TypeScript 3.1.6
Actual behavior:
TS2604: JSX element type 'Comp' does not have any construct or call signatures.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 122
- Comments: 29 (6 by maintainers)
for me the solution was:
This is missing the rest of the error message but, to be sure, make sure you are on the latest version of
@types/reactand that there are no duplicates of it left around in your node_modules or lock file.EDIT: you probably mean to use ComponentType and not ReactType
I might have misunderstood the thread, but I wanted to pass a reference of a jsx Element to another jsx element:
Together with:
Running into a problem that might be related. I have the following component:
which results in
error TS2604: JSX element type 'TagName' doesnothave any construct or call signatures.while
is completely acceptable to the typescript compiler. As is:
Where the only difference in
MaybeLabel2is that I’m usingspaninstead oflabel(usingspaninstead ofdivalso seems acceptable).MaybeLabel3makes it even stranger, as that should be exactly whatMaybeLabelcompiles to.Using latest version of @types/react and @types/react-dom, and verified the problem in typescript 3.2.1, 3.2.2 and 3.3.0-dev.20181219. In 3.1.6 it all works as expected (none of the examples produce errors)
Instead of
Component, useComponentClassTypescript 3.2.1
other example
It looks like, the problem is related to new “nullable discriminants” changes. Looks like ts can’t find common interface for those types. Other example
I got this error because I imported default but declared the corresponding export as named instead of default in the
.d.tsfile…confused me for awhileni zhe hui da ye nb haha
niu b
@ericmasiello I ended up using
React.ElementTypefor a dynamically passed in component. My use case is basically the following:@TacB0sS please elaborate.
We have a similar problem, quick example:
I see this behaviour as completely invalid, because
AandBhave the same types. In the same time,BandDhave similar types, butDis not assignable 🤔Code in repo: https://github.com/layershifter/ts-issue
Just wanted to add this here. Not sure if this was already said but if you are doing a hirer order component you want to use the type React.ComponentType
. “https://flow.org/en/docs/react/types/#toc-react-componenttype”
@TacB0sS For me the main trick was adding
ifcondition. Looks like it doesn’t matter whether you useReact.ComponentTypeorReact.ElementTypeCool! What version of Typescript and what version of @types/react do you have installed?
I’m not sure i understand the outcome of this. I have a similar error that I have not been able to resolve. Here is my use case:
and then I’m using it like such in another component like so:
When I run the compiler, i get this error:
Any ideas how to make this work?
When I write this code i get “JSX element type ‘Header’ does not have any construct or call signatures.ts(2604)” error.
I solved this problem by doing any of these two things:
Case 1:
.d.ts file:
React:
Notice that, in order to use the newly typed component, we have to write MyTypedComponent.MyTypedComponent . This may be obvious to some people, but I wasted alot of time when all I had to do was to use the dot operator on the import and reference the component.
Case 2 [just another way of writing Case 1]:
.d.ts file:
React:
Soo, basically, check your exports, default exports, and imports and make sure you are referencing correctly.
I’m very sorry for my English and I hope this helps.
I would probably say @rexpan and @goloveychuk’s error is similar to #28795 and #28768 based on conditional JSX constructor.
That is because I improved the type of
ReactTypeto exclude components that would not be able to receive the props you are giving it. Because none of the DOM elements can receive{ a: string | undefined }, they are all excluded, and only function/class components are still allowed to be assigned to it.