Skip to content

Type checker hangs on recursive type with arrays #41771

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
judofyr opened this issue Dec 2, 2020 · 1 comment · Fixed by #42448
Closed

Type checker hangs on recursive type with arrays #41771

judofyr opened this issue Dec 2, 2020 · 1 comment · Fixed by #42448
Assignees
Labels
Breaking Change Would introduce errors in existing code Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@judofyr
Copy link

judofyr commented Dec 2, 2020

TypeScript Version: 4.1.2

Search Terms:

Code

type BuildTuple<L extends number, T extends any[] = [any]> = 
    T['length'] extends L ? T : BuildTuple<L, [...T, ...T]>;

type A = BuildTuple<3>

Expected behavior:

The type checker completes in a reasonable time, either by succeeding or by reporting "Type instantiation is excessively deep and possibly infinite".

Actual behavior:

The type checker just hangs.

Playground Link:

https://www.typescriptlang.org/play?ts=4.1.2#code/C4TwDgpgBAQgrgSwDYBMAqcxIgHgDJQQAewEAdigM5RlwC2ARhAE4A0UahJ5VUAhmRABtALpQAvFCECQIgHwSoAKCiqOQgOTYyAc2AALDWOKkK1AgH4OUAFyxEqDFlx52QgHSe07T+7TyAbiUlUEgoAEFFeGR0TGwcAGY5JSA

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Dec 2, 2020
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 4.2.0 milestone Dec 2, 2020
@andrewbranch
Copy link
Member

Hmm, so the problem is that every time we instantiate the false type, the length of the T argument doubles because of the two spreads. The instantiation depth limit is 50, which means by the time we hit it, we’re dealing with a tuple with length 250 ≈ 1.13×1015, which is a pretty long tuple. (I assume this does eventually terminate by running out of memory, but didn’t feel like sticking around to see how long that takes.)

Should there just be a limit on how many elements we’ll try to spread into a tuple? If we hit that limit, do we issue an error or create an array instead?

@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Jan 21, 2021
@DanielRosenwasser DanielRosenwasser added the Breaking Change Would introduce errors in existing code label Jan 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change Would introduce errors in existing code Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants