Skip to content

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

@mingcenwei

Description

@mingcenwei

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions