Skip to content

Stack overflow upon instantiating unbounded recursive type #39495

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

Closed
kasperisager opened this issue Jul 8, 2020 · 0 comments · Fixed by #39552
Closed

Stack overflow upon instantiating unbounded recursive type #39495

kasperisager opened this issue Jul 8, 2020 · 0 comments · Fixed by #39552
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

@kasperisager
Copy link

kasperisager commented Jul 8, 2020

TypeScript Version: 4.0.0-dev.20200615

Search Terms: recursive type, maximum call stack size exceeded

Code

type N<T, K extends string> = T | { [P in K]: N<T, K> }[K];

type M = N<number, "M">;

Expected behavior: To be honest, I'm not entirely sure what I would expect the type M to be. Prior to 3.9, M resolves to unknown due to what I guess is the compiler giving up on instantiating the type which at least avoids a stack overflow. In this particular case, M can only ever be number regardless of how times the recursion is unfolded. Either way, I would not expect the compiler to crash.

Actual behavior: The compiler crashes due to a stack overflow when attempting to instantiate M.

Playground Link: https://www.typescriptlang.org/play/?ts=4.0.0-dev.20200615&ssl=1&ssc=1&pln=3&pc=25#code/C4TwDgpgBAcgPAFQDRQNJQgD2BAdgEwGcpDgAnAS1wHMA+KAXigSgB8oBvKAbQAUoqaALoAuWIhSp6AX26ohAbgBQS0JCgBZRuNwBXALYAjCGRQAiDWdoKgA

Related Issues: #38825

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Crash For flagging bugs which are compiler or service crashes or unclean exits, rather than bad output labels Jul 9, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.0.1 milestone Jul 9, 2020
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
3 participants