Skip to content

Unlimited Array.prototype.flat depth #55686

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
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/lib/es2019.array.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
type FlatArray<Arr, Depth extends number> = {
done: Arr;
recur: Arr extends ReadonlyArray<infer InnerArr> ? FlatArray<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;
}[Depth extends -1 ? "done" : "recur"];
type Integer = any[];

type Increment<N extends Integer> = [...N, any];

type FlattenArrayByOneLevel <Arr> = Arr extends ReadonlyArray<infer InnerArr> ? InnerArr : Arr;

type FlatArray<Arr, Depth extends number, Counter extends Integer = []> =
Arr extends ReadonlyArray<any>
? (
Counter['length'] extends 999
? unknown
: Counter['length'] extends Depth
? FlattenArrayByOneLevel<Arr>
: FlatArray<FlattenArrayByOneLevel<Arr>, Depth, Increment<Counter>>
)
: Arr;

interface ReadonlyArray<T> {
/**
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/arrayFlatNoCrashInference.types
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

=== arrayFlatNoCrashInference.ts ===
function foo<T>(arr: T[], depth: number) {
>foo : <T>(arr: T[], depth: number) => FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>foo : <T>(arr: T[], depth: number) => T[]
>arr : T[]
>depth : number

return arr.flat(depth);
>arr.flat(depth) : FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>arr.flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>arr.flat(depth) : T[]
>arr.flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D, []>[]
>arr : T[]
>flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D, []>[]
>depth : number
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ function foo(arr, depth) {


//// [arrayFlatNoCrashInferenceDeclarations.d.ts]
declare function foo<T>(arr: T[], depth: number): FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[];
declare function foo<T>(arr: T[], depth: number): T[];
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

=== arrayFlatNoCrashInferenceDeclarations.ts ===
function foo<T>(arr: T[], depth: number) {
>foo : <T>(arr: T[], depth: number) => FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>foo : <T>(arr: T[], depth: number) => T[]
>arr : T[]
>depth : number

return arr.flat(depth);
>arr.flat(depth) : FlatArray<T, 0 | 1 | -1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20>[]
>arr.flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>arr.flat(depth) : T[]
>arr.flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D, []>[]
>arr : T[]
>flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D>[]
>flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D, []>[]
>depth : number
}
11 changes: 11 additions & 0 deletions tests/baselines/reference/arrayFlatUnlimitedDepth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//// [tests/cases/compiler/arrayFlatUnlimitedDepth.ts] ////

//// [arrayFlatUnlimitedDepth.ts]
const foo = [ [ ['string' ] ] ]
const flatFoo : string[] = foo.flat(1000)


//// [arrayFlatUnlimitedDepth.js]
"use strict";
var foo = [[['string']]];
var flatFoo = foo.flat(1000);
12 changes: 12 additions & 0 deletions tests/baselines/reference/arrayFlatUnlimitedDepth.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//// [tests/cases/compiler/arrayFlatUnlimitedDepth.ts] ////

=== arrayFlatUnlimitedDepth.ts ===
const foo = [ [ ['string' ] ] ]
>foo : Symbol(foo, Decl(arrayFlatUnlimitedDepth.ts, 0, 5))

const flatFoo : string[] = foo.flat(1000)
>flatFoo : Symbol(flatFoo, Decl(arrayFlatUnlimitedDepth.ts, 1, 5))
>foo.flat : Symbol(Array.flat, Decl(lib.es2019.array.d.ts, --, --))
>foo : Symbol(foo, Decl(arrayFlatUnlimitedDepth.ts, 0, 5))
>flat : Symbol(Array.flat, Decl(lib.es2019.array.d.ts, --, --))

18 changes: 18 additions & 0 deletions tests/baselines/reference/arrayFlatUnlimitedDepth.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//// [tests/cases/compiler/arrayFlatUnlimitedDepth.ts] ////

=== arrayFlatUnlimitedDepth.ts ===
const foo = [ [ ['string' ] ] ]
>foo : string[][][]
>[ [ ['string' ] ] ] : string[][][]
>[ ['string' ] ] : string[][]
>['string' ] : string[]
>'string' : "string"

const flatFoo : string[] = foo.flat(1000)
>flatFoo : string[]
>foo.flat(1000) : string[]
>foo.flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D, []>[]
>foo : string[][][]
>flat : <A, D extends number = 1>(this: A, depth?: D | undefined) => FlatArray<A, D, []>[]
>1000 : 1000

Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ exports.fun = fun;


//// [declarationEmitDistributiveConditionalWithInfer.d.ts]
export declare const fun: (subFun: <Collection, Field extends keyof Collection>() => (Collection[Field] extends infer T ? T extends Collection[Field] ? T extends readonly (infer InnerArr)[] ? InnerArr : T : never : never)[]) => void;
export declare const fun: (subFun: <Collection, Field extends keyof Collection>() => FlatArray<Collection[Field], 0, []>[]) => void;
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
=== declarationEmitDistributiveConditionalWithInfer.ts ===
// This function's type is changed on declaration
export const fun = (
>fun : (subFun: <Collection, Field extends keyof Collection>() => (Collection[Field] extends readonly (infer InnerArr)[] ? InnerArr : Collection[Field])[]) => void
>( subFun: <Collection, Field extends keyof Collection>() => FlatArray<Collection[Field], 0>[]) => { } : (subFun: <Collection, Field extends keyof Collection>() => (Collection[Field] extends readonly (infer InnerArr)[] ? InnerArr : Collection[Field])[]) => void
>fun : (subFun: <Collection, Field extends keyof Collection>() => FlatArray<Collection[Field], 0, []>[]) => void
>( subFun: <Collection, Field extends keyof Collection>() => FlatArray<Collection[Field], 0>[]) => { } : (subFun: <Collection, Field extends keyof Collection>() => FlatArray<Collection[Field], 0, []>[]) => void

subFun: <Collection, Field extends keyof Collection>()
>subFun : <Collection, Field extends keyof Collection>() => (Collection[Field] extends readonly (infer InnerArr)[] ? InnerArr : Collection[Field])[]
>subFun : <Collection, Field extends keyof Collection>() => FlatArray<Collection[Field], 0, []>[]

=> FlatArray<Collection[Field], 0>[]) => { };

This file was deleted.

16 changes: 8 additions & 8 deletions tests/baselines/reference/flatArrayNoExcessiveStackDepth.types
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,17 @@ const repro_43249 = (value: unknown) => {

function f<Arr, D extends number>(x: FlatArray<Arr, any>, y: FlatArray<Arr, D>) {
>f : <Arr, D extends number>(x: FlatArray<Arr, any>, y: FlatArray<Arr, D>) => void
>x : FlatArray<Arr, any>
>y : FlatArray<Arr, D>
>x : FlatArray<Arr, any, []>
>y : FlatArray<Arr, D, []>

x = y;
>x = y : FlatArray<Arr, D>
>x : FlatArray<Arr, any>
>y : FlatArray<Arr, D>
>x = y : FlatArray<Arr, D, []>
>x : FlatArray<Arr, any, []>
>y : FlatArray<Arr, D, []>

y = x; // Error
>y = x : Arr extends readonly (infer InnerArr)[] ? FlatArray<InnerArr, 0 | 2 | 1 | -1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20> : Arr
>y : FlatArray<Arr, D>
>x : Arr extends readonly (infer InnerArr)[] ? FlatArray<InnerArr, 0 | 2 | 1 | -1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20> : Arr
>y = x : FlatArray<Arr, any, []>
>y : FlatArray<Arr, D, []>
>x : FlatArray<Arr, any, []>
}

Loading