You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JSX element type does not have any construct or call signatures in v3.2.0-rc #28631
This is missing the rest of the error message but, to be sure, make sure you are on the latest version of @types/react and 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
Reacted by RyanDagg, K, Thyago Weber, esmadrider (凝尘), Raymond Tijhaar, clems36, Anders Kjær Damgaard, jake roth, Caroline, Stevo and 57 more
Reacted by Tatyana Logvynenko, samuelnihoul, sebastian, Gvidas M. and Hassan Bilal
Reacted by esmadrider (凝尘), Brian Kent, Chris, Ken Tan, Artyom, naziamirzanew, Ofek Ashery, Guy Barnard, Thanh Nguyen, Jonatan Lins and 7 more
Reacted by Nicollas Linhares, Awth Wathje, allenjax and aligithubpleasedontbanbro
I have make sure using the latest @types/react 16.7.7 and no duplicates @types/react in yarn.lock.
If the issue is because of duplication of @types/react, it will return the error Duplication of definition ....
I revert back to "typescript": "^3.1.6" then it work normally.
interfaceP1{p?: booleanc?: string}interfaceP2{p?: booleanc?: any// if you replace c with string, error goes awayd?: any}declarevarC: React.ComponentType<P1>|React.ComponentType<P2>consta=<Cp={true}/>// element does not have any ...
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
interfaceP1{p?: anyc?: string// remove this and it's ok}interfaceP2{p?: boolean}
Reacted by kuzhelov, Jacob Eggers, Shinobu Hayashi, Žilvinas, Nikita Sivukhin, Filip Rydzi, Daniel H Kloke, Ilko Kacharov, Divine Hycenth, Venkateshwar and 12 more
import*asReactfrom'react'//// Types//interfaceConfig<P>{ElementType: React.ReactType<P>}interfaceEmptyProps{}interfaceProps{a?: string}typeHProps={configEmpty: Config<EmptyProps>,config: Config<Props>}//// Component//constH: React.FC<HProps>=({ config, configEmpty })=>{constA: React.ReactType<EmptyProps>=configEmpty.ElementType// assignedconstB: React.ReactType<EmptyProps>='div'// assignedconstC: React.ReactType<Props>=config.ElementType// assignedconstD: React.ReactType<Props>='div'// in this case assignment failedreturn(<div><A/>{/* TS2604: JSX element type 'A' does not have any construct or call signatures. */}<B/><C/><D/></div>)}exportdefaultH
I see this behaviour as completely invalid, because A and B have the same types. In the same time, B and D have similar types, but D is not assignable 🤔
They do not, because you're using const. The type of A is typeof configEmpty.ElementType, but the type of B is 'div'. Using lets will show the same behavior on both.
That is because I improved the type of ReactType to 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.
@Kovensky thank you 👍 Now the issue with assignment is clear for me, but what about this?
import*asReactfrom'react'//// Types//interfaceConfig<P>{ElementType: React.ReactType<P>}interfaceEmpty{}interfaceProps{a?: string}typeHProps={configEmpty: Config<Empty>,config: Config<Props>}//// Component//constH: React.FC<HProps>=({ config, configEmpty })=>{constA: React.ReactType<Empty>=configEmpty.ElementType// is React.ReactType<Empty>constB: React.ReactType<Empty>='div'// is stringconstC: React.ReactType<Props>=config.ElementType// is React.ReactType<Props>return(<div>{/* React.ReactType<Empty> */}<A/>{/* <--- TS2604: JSX element type 'A' does not have any construct or call signatures. */}{/* React.ReactType<Empty> */}<B/>{/* React.ReactType<Props> */}<C/></div>)}exportdefaultH
A and B has the same obviously defined types, why A fails? It's really confusing thing.
Hm, so the issue is that ReactType<any> returns a union of every builtin jsx tag (plus ComponentType<any>, but that part's not problematic), and the signatures for those components do not trivially simplify (there is not a signature that perfectly encompasses all the others). This, too, relies on #7294 to be fixed. On the plus side, the underlying change needed to fix for all these JSX issues that get reported is the same.
For context, we effectively manufacture a type like this:
Reacted by Maxime Bernard, Tom-Olav Bøyum, Brayden Tidwell, Ilyas Assainov, Ken Schnall, Alex Cornellier, 流浪大法师, John Grant, Richard, Ramanpreet Singh and 1 more
I might have misunderstood the thread, but I wanted to pass a reference of a jsx Element to another jsx element:
exportconstAppWrapper=hot(module)((props: WrapperProps)=>{constMainApp=props.mainApp;if(!MainApp)// <-- JSX elements MUST start with upper case!!thrownewImplementationMissingException("mainApp was not specified!!");return(<Routerhistory={BrowserHistoryModule.getHistory()}><MainAppprop1={"value"}/></Router>)});
Reacted by Bilal Iftikhar, Neil Agarwal, Warao, Lukáš Rod, Ugo Onali, Denys Yalomist, Sora Lee, Jesse Shawl, Georgina Gilbert-Hoare, 流浪大法师 and 2 more
Reacted by Nikolai Hegelstad, Mo Doaie, paul dariye, Andrei Xavier de Oliveira Calazans, hyperh, Haley Mendoza, Julien Viala, Jose Miguel Bejarano, Bilal Iftikhar, sunpietro and 4 more
Adam van der Kruk (@TacB0sS) For me the main trick was adding if condition. Looks like it doesn't matter whether you use React.ComponentType or React.ElementType
I got this error because I imported default but declared the corresponding export as named instead of default in the .d.ts file...confused me for awhile
Reacted by Alex Kwiatkowski, Juan Valencia, Jason Hoch, George Cox, Julio M Saito, morzel85, magarampage, Nafis, Adán Carrasco, 流浪大法师 and 4 more
Reacted by Alex Kwiatkowski, Jean-Philippe Pellerin, Renan San, Julio M Saito, 流浪大法师 and Rajesh Kanna
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
importMyTypedComponentfrom"foo";functionAnotherComponent(){/* Notice in here we have to use the dot operator and reference the component */return<MyTypedComponent.MyTypedComponent/>}
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:
declare module "foo"{interfacePropFoo{propy: string;}exportdefaultclassMyTypedComponentextendsReact.Component<PropFoo>{}//Notice the export default in here}
React:
importMyTypedComponentfrom"foo";functionAnotherComponent(){/* Since this component is default exported, no need to use the dot operator */return<MyTypedComponent/>}
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.
Activity
Jessidhia commentedon Nov 23, 2018
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
rexpan commentedon Nov 23, 2018
@Kovensky
I have make sure using the latest
@types/react 16.7.7and no duplicates@types/reactinyarn.lock.If the issue is because of duplication of
@types/react, it will return the errorDuplication of definition ....I revert back to
"typescript": "^3.1.6"then it work normally.goloveychuk commentedon Nov 30, 2018
Typescript 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
layershifter commentedon Dec 6, 2018
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
Jessidhia commentedon Dec 6, 2018
They do not, because you're using
const. The type ofAistypeof configEmpty.ElementType, but the type ofBis'div'. Usinglets will show the same behavior on both.layershifter commentedon Dec 6, 2018
@Kovensky what about
AandC? WhyCworks whileAfails withdoes not have any constructerror?Jessidhia commentedon Dec 6, 2018
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.layershifter commentedon Dec 6, 2018
@Kovensky thank you 👍 Now the issue with assignment is clear for me, but what about this?
AandBhas the same obviously defined types, whyAfails? It's really confusing thing.azizhk commentedon Dec 6, 2018
I would probably say Vien Luong (@rexpan) and Vadym Holoveichuk (@goloveychuk)'s error is similar to #28795 and #28768 based on conditional JSX constructor.
weswigham commentedon Dec 10, 2018
Hm, so the issue is that
ReactType<any>returns a union of every builtin jsx tag (plusComponentType<any>, but that part's not problematic), and the signatures for those components do not trivially simplify (there is not a signature that perfectly encompasses all the others). This, too, relies on #7294 to be fixed. On the plus side, the underlying change needed to fix for all these JSX issues that get reported is the same.For context, we effectively manufacture a type like this:
which ends up as being a union of a ton of unique signatures.
6 remaining items
hegelstad commentedon Mar 27, 2019
Adam van der Kruk (@TacB0sS) please elaborate.
TacB0sS commentedon Mar 27, 2019
I might have misunderstood the thread, but I wanted to pass a reference of a jsx Element to another jsx element:
Together with:
orest22 commentedon Apr 25, 2019
Adam van der Kruk (@TacB0sS) For me the main trick was adding
ifcondition. Looks like it doesn't matter whether you useReact.ComponentTypeorReact.ElementTypeericmasiello commentedon May 24, 2019
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?
pinkbunny1 commentedon Nov 22, 2019
Instead of
Component, useComponentClassjedwards1211 commentedon Dec 13, 2019
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 awhileSARCASMOOO commentedon Aug 12, 2020
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"
hellatan commentedon Oct 5, 2020
Eric Masiello (@ericmasiello) I ended up using
React.ElementTypefor a dynamically passed in component. My use case is basically the following:ericmasiello commentedon Oct 5, 2020
Cool! What version of Typescript and what version of @types/react do you have installed?
hellatan commentedon Oct 5, 2020
Eric Masiello (@ericmasiello)
TypeScript@3.9.7
@types/react@16.9.49
LuisDev99 commentedon Nov 8, 2020
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.
zhuxchong commentedon Nov 25, 2020
niu b
mancoxu commentedon Jan 22, 2021
ni zhe hui da ye nb haha