Skip to content

Fix unsound Array.concat() typings #33620

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 2 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
1 change: 1 addition & 0 deletions src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -950,6 +950,7 @@ namespace FourSlashInterface {
varEntry("JSON"),
interfaceEntry("ReadonlyArray"),
interfaceEntry("ConcatArray"),
typeEntry("Flatten"),
varEntry("Array"),
interfaceEntry("ArrayConstructor"),
interfaceEntry("TypedPropertyDescriptor"),
Expand Down
5 changes: 3 additions & 2 deletions src/lib/es5.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ interface ReadonlyArray<T> {
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: (T | ConcatArray<T>)[]): T[];
concat<U extends (T | ConcatArray<T>)[]>(...items: U): (T | Flatten<U[number]>)[];
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
Expand Down Expand Up @@ -1187,6 +1187,7 @@ interface ConcatArray<T> {
join(separator?: string): string;
slice(start?: number, end?: number): T[];
}
type Flatten<T> = T extends ConcatArray<infer U> ? U : T;

interface Array<T> {
/**
Expand Down Expand Up @@ -1219,7 +1220,7 @@ interface Array<T> {
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: (T | ConcatArray<T>)[]): T[];
concat<U extends (T | ConcatArray<T>)[]>(...items: U): (T | Flatten<U[number]>)[];
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/arrayConcat2.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ a.concat('Hello');

var b = new Array<string>();
b.concat('hello');

// #19535

const [x] = (undefined as unknown as string[][]).concat([""]);
x == "";


//// [arrayConcat2.js]
Expand All @@ -14,3 +19,6 @@ a.concat("hello", 'world');
a.concat('Hello');
var b = new Array();
b.concat('hello');
// #19535
var x = undefined.concat([""])[0];
x == "";
11 changes: 11 additions & 0 deletions tests/baselines/reference/arrayConcat2.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,14 @@ b.concat('hello');
>b : Symbol(b, Decl(arrayConcat2.ts, 5, 3))
>concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

// #19535

const [x] = (undefined as unknown as string[][]).concat([""]);
>x : Symbol(x, Decl(arrayConcat2.ts, 10, 7))
>(undefined as unknown as string[][]).concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>undefined : Symbol(undefined)
>concat : Symbol(Array.concat, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

x == "";
>x : Symbol(x, Decl(arrayConcat2.ts, 10, 7))

31 changes: 25 additions & 6 deletions tests/baselines/reference/arrayConcat2.types
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ var a: string[] = [];

a.concat("hello", 'world');
>a.concat("hello", 'world') : string[]
>a.concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
>a.concat : { (...items: ConcatArray<string>[]): string[]; <U extends (string | ConcatArray<string>)[]>(...items: U): (string | Flatten<U[number]>)[]; }
>a : string[]
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
>concat : { (...items: ConcatArray<string>[]): string[]; <U extends (string | ConcatArray<string>)[]>(...items: U): (string | Flatten<U[number]>)[]; }
>"hello" : "hello"
>'world' : "world"

a.concat('Hello');
>a.concat('Hello') : string[]
>a.concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
>a.concat : { (...items: ConcatArray<string>[]): string[]; <U extends (string | ConcatArray<string>)[]>(...items: U): (string | Flatten<U[number]>)[]; }
>a : string[]
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
>concat : { (...items: ConcatArray<string>[]): string[]; <U extends (string | ConcatArray<string>)[]>(...items: U): (string | Flatten<U[number]>)[]; }
>'Hello' : "Hello"

var b = new Array<string>();
Expand All @@ -25,8 +25,27 @@ var b = new Array<string>();

b.concat('hello');
>b.concat('hello') : string[]
>b.concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
>b.concat : { (...items: ConcatArray<string>[]): string[]; <U extends (string | ConcatArray<string>)[]>(...items: U): (string | Flatten<U[number]>)[]; }
>b : string[]
>concat : { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; }
>concat : { (...items: ConcatArray<string>[]): string[]; <U extends (string | ConcatArray<string>)[]>(...items: U): (string | Flatten<U[number]>)[]; }
>'hello' : "hello"

// #19535

const [x] = (undefined as unknown as string[][]).concat([""]);
>x : string | string[]
>(undefined as unknown as string[][]).concat([""]) : (string | string[])[]
>(undefined as unknown as string[][]).concat : { (...items: ConcatArray<string[]>[]): string[][]; <U extends (string[] | ConcatArray<string[]>)[]>(...items: U): (string[] | Flatten<U[number]>)[]; }
>(undefined as unknown as string[][]) : string[][]
>undefined as unknown as string[][] : string[][]
>undefined as unknown : unknown
>undefined : undefined
>concat : { (...items: ConcatArray<string[]>[]): string[][]; <U extends (string[] | ConcatArray<string[]>)[]>(...items: U): (string[] | Flatten<U[number]>)[]; }
>[""] : string[]
>"" : ""

x == "";
>x == "" : boolean
>x : string | string[]
>"" : ""

4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayConcat3.types
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ function doStuff<T extends object, T1 extends T>(a: Array<Fn<T>>, b: Array<Fn<T1

b.concat(a);
>b.concat(a) : Fn<T1>[]
>b.concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; (...items: (Fn<T1> | ConcatArray<Fn<T1>>)[]): Fn<T1>[]; }
>b.concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; <U extends (Fn<T1> | ConcatArray<Fn<T1>>)[]>(...items: U): (Fn<T1> | Flatten<U[number]>)[]; }
>b : Fn<T1>[]
>concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; (...items: (Fn<T1> | ConcatArray<Fn<T1>>)[]): Fn<T1>[]; }
>concat : { (...items: ConcatArray<Fn<T1>>[]): Fn<T1>[]; <U extends (Fn<T1> | ConcatArray<Fn<T1>>)[]>(...items: U): (Fn<T1> | Flatten<U[number]>)[]; }
>a : Fn<T>[]
}

4 changes: 2 additions & 2 deletions tests/baselines/reference/arrayConcatMap.types
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ var x = [].concat([{ a: 1 }], [{ a: 2 }])
>[].concat([{ a: 1 }], [{ a: 2 }]) .map(b => b.a) : any[]
>[].concat([{ a: 1 }], [{ a: 2 }]) .map : <U>(callbackfn: (value: any, index: number, array: any[]) => U, thisArg?: any) => U[]
>[].concat([{ a: 1 }], [{ a: 2 }]) : any[]
>[].concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
>[].concat : { (...items: ConcatArray<any>[]): any[]; <U extends any[]>(...items: U): any[]; }
>[] : undefined[]
>concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
>concat : { (...items: ConcatArray<any>[]): any[]; <U extends any[]>(...items: U): any[]; }
>[{ a: 1 }] : { a: number; }[]
>{ a: 1 } : { a: number; }
>a : number
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(13,1): error TS2322: Type 'A[]' is not assignable to type 'readonly B[]'.
Property 'b' is missing in type 'A' but required in type 'B'.
tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error TS2322: Type 'C<A>' is not assignable to type 'readonly B[]'.
The types returned by 'concat(...)' are incompatible between these types.
The types returned by 'slice(...)' are incompatible between these types.
Type 'A[]' is not assignable to type 'B[]'.
Type 'A' is not assignable to type 'B'.

Expand Down Expand Up @@ -31,7 +31,7 @@ tests/cases/compiler/arrayOfSubtypeIsAssignableToReadonlyArray.ts(18,1): error T
rrb = cra; // error: 'A' is not assignable to 'B'
~~~
!!! error TS2322: Type 'C<A>' is not assignable to type 'readonly B[]'.
!!! error TS2322: The types returned by 'concat(...)' are incompatible between these types.
!!! error TS2322: The types returned by 'slice(...)' are incompatible between these types.
!!! error TS2322: Type 'A[]' is not assignable to type 'B[]'.
!!! error TS2322: Type 'A' is not assignable to type 'B'.

8 changes: 4 additions & 4 deletions tests/baselines/reference/concatError.types
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ fa = fa.concat([0]);
>fa = fa.concat([0]) : number[]
>fa : number[]
>fa.concat([0]) : number[]
>fa.concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>fa.concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>fa : number[]
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>[0] : number[]
>0 : 0

fa = fa.concat(0);
>fa = fa.concat(0) : number[]
>fa : number[]
>fa.concat(0) : number[]
>fa.concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>fa.concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>fa : number[]
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>0 : 0


Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/concatTuples.types
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ ijs = ijs.concat([[3, 4], [5, 6]]);
>ijs = ijs.concat([[3, 4], [5, 6]]) : [number, number][]
>ijs : [number, number][]
>ijs.concat([[3, 4], [5, 6]]) : [number, number][]
>ijs.concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; (...items: ([number, number] | ConcatArray<[number, number]>)[]): [number, number][]; }
>ijs.concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; <U extends ([number, number] | ConcatArray<[number, number]>)[]>(...items: U): ([number, number] | Flatten<U[number]>)[]; }
>ijs : [number, number][]
>concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; (...items: ([number, number] | ConcatArray<[number, number]>)[]): [number, number][]; }
>concat : { (...items: ConcatArray<[number, number]>[]): [number, number][]; <U extends ([number, number] | ConcatArray<[number, number]>)[]>(...items: U): ([number, number] | Flatten<U[number]>)[]; }
>[[3, 4], [5, 6]] : [number, number][]
>[3, 4] : [number, number]
>3 : 3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,15 @@ export default class Operation {

// Commenting out this line will fix the problem.
result = (result || []).concat(innerResult);
>result = (result || []).concat(innerResult) : IValidationError[]
>result = (result || []).concat(innerResult) : any[]
>result : IValidationError[] | null
>(result || []).concat(innerResult) : IValidationError[]
>(result || []).concat : { (...items: ConcatArray<IValidationError>[]): IValidationError[]; (...items: (IValidationError | ConcatArray<IValidationError>)[]): IValidationError[]; }
>(result || []).concat(innerResult) : any[]
>(result || []).concat : { (...items: ConcatArray<IValidationError>[]): IValidationError[]; <U extends (IValidationError | ConcatArray<IValidationError>)[]>(...items: U): (IValidationError | Flatten<U[number]>)[]; }
>(result || []) : IValidationError[]
>result || [] : IValidationError[]
>result : IValidationError[] | null
>[] : never[]
>concat : { (...items: ConcatArray<IValidationError>[]): IValidationError[]; (...items: (IValidationError | ConcatArray<IValidationError>)[]): IValidationError[]; }
>concat : { (...items: ConcatArray<IValidationError>[]): IValidationError[]; <U extends (IValidationError | ConcatArray<IValidationError>)[]>(...items: U): (IValidationError | Flatten<U[number]>)[]; }
>innerResult : any
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ tests/cases/conformance/es6/destructuring/destructuringParameterDeclaration4.ts(
a1(...array2); // Error parameter type is (number|string)[]
~~~~~~
!!! error TS2552: Cannot find name 'array2'. Did you mean 'Array'?
!!! related TS2728 /.ts/lib.es5.d.ts:1385:13: 'Array' is declared here.
!!! related TS2728 /.ts/lib.es5.d.ts:1386:13: 'Array' is declared here.
a5([1, 2, "string", false, true]); // Error, parameter type is [any, any, [[any]]]
~~~~~~~~
!!! error TS2322: Type 'string' is not assignable to type '[[any]]'.
Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/destructuringTuple.types
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ const [oops1] = [1, 2, 3].reduce((accu, el) => accu.concat(el), []);
>accu : []
>el : number
>accu.concat(el) : any
>accu.concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
>accu.concat : { (...items: ConcatArray<never>[]): never[]; <U extends ConcatArray<never>[]>(...items: U): Flatten<U[number]>[]; }
>accu : []
>concat : { (...items: ConcatArray<never>[]): never[]; (...items: ConcatArray<never>[]): never[]; }
>concat : { (...items: ConcatArray<never>[]): never[]; <U extends ConcatArray<never>[]>(...items: U): Flatten<U[number]>[]; }
>el : number
>[] : []

Expand All @@ -54,9 +54,9 @@ const [oops2] = [1, 2, 3].reduce((acc: number[], e) => acc.concat(e), []);
>acc : number[]
>e : number
>acc.concat(e) : number[]
>acc.concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>acc.concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>acc : number[]
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>e : number
>[] : never[]

Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ function rebase(fn: (base: any, ...args: any[]) => any): (...args: any[]) => any
>apply : (this: Function, thisArg: any, argArray?: any) => any
>this : any
>[ this ].concat(args) : any[]
>[ this ].concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
>[ this ].concat : { (...items: ConcatArray<any>[]): any[]; <U extends any[]>(...items: U): any[]; }
>[ this ] : any[]
>this : any
>concat : { (...items: ConcatArray<any>[]): any[]; (...items: any[]): any[]; }
>concat : { (...items: ConcatArray<any>[]): any[]; <U extends any[]>(...items: U): any[]; }
>args : any[]

};
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/intersectionTypeInference3.types
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ declare const b: Set<A>;
const c1 = Array.from(a).concat(Array.from(b));
>c1 : Nominal<"A", string>[]
>Array.from(a).concat(Array.from(b)) : Nominal<"A", string>[]
>Array.from(a).concat : { (...items: ConcatArray<Nominal<"A", string>>[]): Nominal<"A", string>[]; (...items: (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]): Nominal<"A", string>[]; }
>Array.from(a).concat : { (...items: ConcatArray<Nominal<"A", string>>[]): Nominal<"A", string>[]; <U extends (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]>(...items: U): (Nominal<"A", string> | Flatten<U[number]>)[]; }
>Array.from(a) : Nominal<"A", string>[]
>Array.from : { <T>(arrayLike: ArrayLike<T>): T[]; <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; <T>(iterable: Iterable<T> | ArrayLike<T>): T[]; <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; }
>Array : ArrayConstructor
>from : { <T>(arrayLike: ArrayLike<T>): T[]; <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; <T>(iterable: Iterable<T> | ArrayLike<T>): T[]; <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; }
>a : Set<Nominal<"A", string>>
>concat : { (...items: ConcatArray<Nominal<"A", string>>[]): Nominal<"A", string>[]; (...items: (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]): Nominal<"A", string>[]; }
>concat : { (...items: ConcatArray<Nominal<"A", string>>[]): Nominal<"A", string>[]; <U extends (Nominal<"A", string> | ConcatArray<Nominal<"A", string>>)[]>(...items: U): (Nominal<"A", string> | Flatten<U[number]>)[]; }
>Array.from(b) : Nominal<"A", string>[]
>Array.from : { <T>(arrayLike: ArrayLike<T>): T[]; <T, U>(arrayLike: ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; <T>(iterable: Iterable<T> | ArrayLike<T>): T[]; <T, U>(iterable: Iterable<T> | ArrayLike<T>, mapfn: (v: T, k: number) => U, thisArg?: any): U[]; }
>Array : ArrayConstructor
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/iteratorSpreadInArray6.types
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ var array: number[] = [0, 1];

array.concat([...new SymbolIterator]);
>array.concat([...new SymbolIterator]) : any
>array.concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>array.concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>array : number[]
>concat : { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; }
>concat : { (...items: ConcatArray<number>[]): number[]; <U extends (number | ConcatArray<number>)[]>(...items: U): (number | Flatten<U[number]>)[]; }
>[...new SymbolIterator] : symbol[]
>...new SymbolIterator : symbol
>new SymbolIterator : SymbolIterator
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/iteratorSpreadInArray7.types
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ var array: symbol[];

array.concat([...new SymbolIterator]);
>array.concat([...new SymbolIterator]) : symbol[]
>array.concat : { (...items: ConcatArray<symbol>[]): symbol[]; (...items: (symbol | ConcatArray<symbol>)[]): symbol[]; }
>array.concat : { (...items: ConcatArray<symbol>[]): symbol[]; <U extends (symbol | ConcatArray<symbol>)[]>(...items: U): (symbol | Flatten<U[number]>)[]; }
>array : symbol[]
>concat : { (...items: ConcatArray<symbol>[]): symbol[]; (...items: (symbol | ConcatArray<symbol>)[]): symbol[]; }
>concat : { (...items: ConcatArray<symbol>[]): symbol[]; <U extends (symbol | ConcatArray<symbol>)[]>(...items: U): (symbol | Flatten<U[number]>)[]; }
>[...new SymbolIterator] : symbol[]
>...new SymbolIterator : symbol
>new SymbolIterator : SymbolIterator
Expand Down
Loading