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

TypeError: Cannot read property 'length' of undefined #45882

Closed
oxy-plus opened this issue Sep 15, 2021 · 4 comments
Closed

TypeError: Cannot read property 'length' of undefined #45882

oxy-plus opened this issue Sep 15, 2021 · 4 comments
Assignees
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output

Comments

@oxy-plus
Copy link

oxy-plus commented Sep 15, 2021

Bug Report

No error or wrong in the editor when coding.

C:\XXX\node_modules\typescript\lib\tsc.js:90672
                throw e;
                ^

TypeError: Cannot read property 'length' of undefined
    at elaborateDidYouMeanToCallOrConstruct (C:\XXX\node_modules\typescript\lib\tsc.js:49543:72)
    at elaborateError (C:\XXX\node_modules\typescript\lib\tsc.js:49507:20)
    at checkTypeRelatedToAndOptionallyElaborate (C:\XXX\node_modules\typescript\lib\tsc.js:49495:32)
    at getSignatureApplicabilityError (C:\XXX\node_modules\typescript\lib\tsc.js:59070:26)
    at resolveCall (C:\XXX\node_modules\typescript\lib\tsc.js:59374:37)
    at resolveCallExpression (C:\XXX\node_modules\typescript\lib\tsc.js:59704:20)
    at resolveSignature (C:\XXX\node_modules\typescript\lib\tsc.js:60026:28)
    at getResolvedSignature (C:\XXX\node_modules\typescript\lib\tsc.js:60046:26)
    at checkCallExpression (C:\XXX\node_modules\typescript\lib\tsc.js:60156:29)
    at checkExpressionWorker (C:\XXX\node_modules\typescript\lib\tsc.js:62512:28)

🔎 Search Terms

🕗 Version & Regression Information

My ts version is 4.4.3.
(I updated ts to the latest version after the problem occurred, and I don't remember the version I used before)

  • This is a crash

⏯ Playground Link

Can't provide.

💻 Code

This is the smallest version I can simplify:

/* NOTE tsconfig.json:
{
	"compilerOptions": {
		"target": "ESNext",
		"module": "commonjs",
		"strict": true,
	}
}
 */

type TMessage<Type extends string = string> = {
	type: Type
}

type TMessageMap<_Type extends string = string> = {
	[K in _Type]: {
		msg: TMessage
		chain: keyof TMessageMap<_Type> | null
	}
}

type GetMsg<
	MsgMap extends TMessageMap,
	Type extends keyof MsgMap = keyof MsgMap
	> = MsgMap[Type]["msg"]

type GetChain<
	MsgMap extends TMessageMap,
	Type extends keyof MsgMap = keyof MsgMap
	> =
	MsgMap[Type]["chain"] extends keyof MsgMap ?
	GetMsg<MsgMap, MsgMap[Type]["chain"]>
	: null

export type TReply<
	MsgMap extends TMessageMap,
	LocalMsg extends GetMsg<MsgMap> | null,
	HandleMsg extends GetMsg<MsgMap>,
	RemoteMsg extends GetMsg<MsgMap> | null
	= (
		LocalMsg extends GetMsg<MsgMap> ?
		GetChain<MsgMap, LocalMsg["type"]>
		: null
	)
	> = (
		RemoteMsg extends GetMsg<MsgMap> ?
		Answer<
			MsgMap,
			RemoteMsg,
			HandleMsg
		>
		: Handle<MsgMap, HandleMsg>
	)

type Answer<
	MsgMap extends TMessageMap,
	RemoteMsg extends GetMsg<MsgMap>,
	HandleMsg extends GetMsg<MsgMap>
	> = {
		answer: (callback: (msg: RemoteMsg) => GetChain<MsgMap, RemoteMsg["type"]>) => TReply<
			MsgMap,
			GetChain<MsgMap, RemoteMsg["type"]>,
			HandleMsg
		>
	}

type Handle<
	MsgMap extends TMessageMap,
	HandleMsg extends GetMsg<MsgMap>
	> = {
		handle: <Msg extends HandleMsg>(callback: (msg: Msg) => GetChain<MsgMap, Msg["type"]>) => void
	}

export function fn<
	MsgMap extends TMessageMap,
	/* NOTE
	if use `keyof MsgMap`, then compilation will continue
	like this:
	Listen extends keyof MsgMap,
	Handle extends keyof MsgMap
	 */
	Listen extends Extract<keyof MsgMap, string>,
	Handle extends Extract<keyof MsgMap, string>
>(
) {
	type TCallback<Type extends Listen> = (
		getReply: () => TReply<
			MsgMap,
			null,
			GetMsg<MsgMap, Handle>,
			GetMsg<MsgMap, Type>
		>
	) => void

	(1 as unknown as TCallback<Listen>)(() => {
		return {} as unknown as TReply<
			MsgMap,
			GetMsg<MsgMap, Listen>,
			GetMsg<MsgMap, Handle>
		>
		/* NOTE ↑↑↑
		I know `as` is used incorrectly here,
		and if correct it, then compilation will continue
		 */
	})
}

🙁 Actual behavior

Build process interrupt.

🙂 Expected behavior

Output file or print error.

@andrewbranch andrewbranch added the Needs More Info The issue still hasn't been fully clarified label Sep 15, 2021
@andrewbranch
Copy link
Member

There’s nothing we can do with so little info. Can you at least tell us what version you were using?

@oxy-plus
Copy link
Author

There’s nothing we can do with so little info. Can you at least tell us what version you were using?

I tried again, and this time I got a single file of 100 lines. I have edited my issue and wrote it in the "Code"

@andrewbranch
Copy link
Member

Thanks @oxy-plus! This is a regression between 4.2 and 4.3.

@andrewbranch andrewbranch added Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output and removed Needs More Info The issue still hasn't been fully clarified labels Sep 15, 2021
@andrewbranch andrewbranch added this to the TypeScript 4.5.1 milestone Sep 15, 2021
@andrewbranch andrewbranch changed the title error when build TypeError: Cannot read property 'length' of undefined Sep 15, 2021
@armanio123
Copy link
Member

Fixed in #41821

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output
Projects
None yet
Development

No branches or pull requests

3 participants