Skip to content

Trying to flatten an array with unknown depth causes a type error #44408

Description

Bug Report

Trying to flatten an array with unknown depth causes a type error.

🔎 Search Terms

  • flat
  • Infinity

🕗 Version & Regression Information

  • Errors in version 4.2.4
  • This changed between versions 3.6.2 and 3.7.5 (but probably just because the type definitions aren't supported prior)

The PR #32131 provides a definition for FlatArray that has this issue.

⏯ Playground Link

Playground link with relevant code

💻 Code

// first define a nested list with arbitrary depthtypeNested<T>=(T|Nested<T>)[];constarr: Nested<number>=[1,[2,[3]]];// locally, this bit fails for me (version 4.2.4)//// with an error '"recur"' is referenced directly or indirectly in its own type annotation.constres: number[]=arr.flat(Infinity);// you can do a quick workaround as follows:constres2: number[]=(arrasany[]).flat(Infinity);// but for some reason on the TypeScript playground this doesn't fail, but you can emulate it// by using the following definition that is equivalent to the one provided in the standard library:// https://github.com/microsoft/TypeScript/blob/d8e9f6951919a347bb155b938a5006f9efabb778/lib/lib.es2019.array.d.ts#L21typeFlatArray2<Arr,Depthextendsnumber>={"done": Arr,"recur": ArrextendsReadonlyArray<infer InnerArr>
? FlatArray2<InnerArr,[-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20][Depth]>
: Arr}[Depthextends-1 ? "done" : "recur"];// causes a type error: '"recur"' is referenced directly or indirectly in its own type annotation.typeTestFlat<T>=FlatArray2<Nested<T>,typeofInfinity>

🙁 Actual behavior

The code errors with '"recur"' is referenced directly or indirectly in its own type annotation.

🙂 Expected behavior

The code to pass typechecking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs InvestigationThis issue needs a team member to investigate its status.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions