Skip to content

Stack overflow upon instantiating unbounded recursive type #39495

Closed

Description

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

Activity

  1. added
    Bug
    Crash
    on Jul 9, 2020
  2. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

BugCrash

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    ⌂ Proxy