Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic React HOC: TypeError: Cannot read property 'declarations' of undefined #41616

Closed
mingcenwei opened this issue Nov 20, 2020 · 6 comments · Fixed by #41758
Closed

Generic React HOC: TypeError: Cannot read property 'declarations' of undefined #41616

mingcenwei opened this issue Nov 20, 2020 · 6 comments · Fixed by #41758
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@mingcenwei
Copy link

TypeScript Version: 4.1.2

Search Terms: TypeError Cannot read property declarations of undefined

Code
src-file1.tsx:

import React from "react";

type Tags = "span" | "div";

export const Hoc = <Tag extends Tags>(
	TagElement: Tag,
): React.FunctionComponent => {
	const Component = () => <TagElement />;
	return Component;
};

src-file2.tsx:

import React from "react";

type Tags = "span" | "div";

export const Hoc = (TagElement: Tags): React.FunctionComponent => {
	const Component = () => <TagElement />;
	return Component;
};

Expected behavior:
Both npx tsc --strict --noEmit --jsx 'react' src-file1.tsx and npx tsc --strict --noEmit --jsx 'react' src-file2.tsx will run successfully.

Actual behavior:
npx tsc --strict --noEmit --jsx 'react' src-file2.tsx runs successfully, but npx tsc --strict --noEmit --jsx 'react' src-file1.tsx fails with the error message (where "$PWD" is the present working directory and "$HOME" is my home directory):

"$PWD"/node_modules/typescript/lib/tsc.js:87001
                throw e;
                ^

TypeError: Cannot read property 'declarations' of undefined
    at addImplementationSuccessElaboration ("$PWD"/node_modules/typescript/lib/tsc.js:57022:116)
    at resolveCall ("$PWD"/node_modules/typescript/lib/tsc.js:56947:33)
    at resolveJsxOpeningLikeElement ("$PWD"/node_modules/typescript/lib/tsc.js:57576:20)
    at resolveSignature ("$PWD"/node_modules/typescript/lib/tsc.js:57597:28)
    at getResolvedSignature ("$PWD"/node_modules/typescript/lib/tsc.js:57608:26)
    at checkJsxOpeningLikeElementOrOpeningFragment ("$PWD"/node_modules/typescript/lib/tsc.js:55477:27)
    at checkJsxSelfClosingElementDeferred ("$PWD"/node_modules/typescript/lib/tsc.js:55058:13)
    at checkDeferredNode ("$PWD"/node_modules/typescript/lib/tsc.js:64402:21)
    at Map.forEach (<anonymous>)
    at checkDeferredNodes ("$PWD"/node_modules/typescript/lib/tsc.js:64373:37)
npm ERR! code 1
npm ERR! path "$PWD"
npm ERR! command failed
npm ERR! command sh -c tsc --strict --noEmit --jsx react --esModuleInterop src-file1.tsx

npm ERR! A complete log of this run can be found in:
npm ERR!    "$HOME"/.npm/_logs/2020-11-20T20_34_21_223Z-debug.log

Playground Link:
Compiling the first source file crashes the web compiler,
image
image
image
while compiling the second source file succeeds.
image
image
image

Related Issues:
#33735 #21817 #21811 #21798 #20475 #6428

@mingcenwei
Copy link
Author

Using React.createElement(TagElement) instead of <TagElement /> is a workaround.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Nov 21, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.1.2 milestone Nov 21, 2020
@capaj
Copy link

capaj commented Nov 25, 2020

we also get this error on our production React app at @LeapLabs

@remcohaszing
Copy link

We’re also getting this error here. This doesn’t use React, but it does use JSX. The relevant code can be found here.

@lkarmelo
Copy link

lkarmelo commented Nov 30, 2020

I get the same issue in simple Button component code when i set "jsx" to "react-jsx" in tsconfig, even if i simplify it to this

const Element = 'button';
return (
   <Element
     //some props
    />
)

Returning just <button /> works fine

@olee
Copy link

olee commented Dec 9, 2020

Could someone perhaps tell me in which version this was introduced so I can downgrade typescript again as minimal as possible?

@remcohaszing
Copy link

This bug was introduced in version 4.1.0 and still exists in 4.1.2
The last working version is 4.0.5
A fix was made in #41758 and will be released in 4.2.0
A merge request to include this in 4.1.x is pending in #41862

Until #41862 is released, people should stick to version 4.0.5.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants