Skip to content

Stack overflow upon instantiating unbounded recursive type #39495

@kasperisager

Description

@kasperisager

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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptCrashFor flagging bugs which are compiler or service crashes or unclean exits, rather than bad output

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions