-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Soundness issue with Array.prototype.concat and arrays of arrays #19535
Comments
const foo: [number, string][] = [[1, 'one']];
foo.push([2, 'two']);
console.log(foo); or const foo: [number, string][] = [[1, 'one']];
console.log(foo.concat([[2, 'two']])); |
This is absolutely true. My concern primarily lies with the fact that it was so easy to violate type safety. It would be nice if this showed up in the editor rather than something I had to debug at runtime. |
One option is to change the definition of concat<U>(...items: (T | ConcatArray<T>)[]): T extends ConcatArray<infer E> ? (E | T)[] : T[]; |
PRs welcomed |
I'm working on it, but I've encountered the following error after doing the change: src/compiler/core.ts:840:42 - error TS2345: Argument of type 'SortedArray<T>' is not assignable to parameter of type 'ReadonlyArray<T>'.
Types of property 'concat' are incompatible.
Type '(...items: (T | ConcatArray<T>)[]) => T extends ConcatArray<infer E> ? (E | T)[] : T[]' is not assignable to type '(...items: (T | ConcatArray<T>)[]) => T extends ConcatArray<infer E> ? (E | T)[] : T[]'. Two different types with this name exist, but they are unrelated.
Type 'T extends ConcatArray<infer E> ? (E | T)[] : T[]' is not assignable to type 'T extends ConcatArray<infer E> ? (E | T)[] : T[]'. Two different types with this name exist, but they are unrelated.
840 const insertIndex = binarySearch(array, insert, identity, compare);
~~~~~ ...and a bunch of errors of the form |
did you change the declaration of both Array and ReadonlyArray? |
Another example: const a: number[][] = [[1]].concat([[2]], [3])
// → [[1], [2], 3] and a related issue with const b: number[][] = [true, false].flatMap<number[]>(x => x ? [1] : [[2]]);
// → [1, [2]] |
Rolling up into #36554 |
TypeScript Version: 2.7.0-dev.201xxxxx
Code
Expected behavior:
Either
[[1, 'one'], [2, 'two']]
or a type error.Actual behavior:
[[1, 'one'], 2, 'two']
The text was updated successfully, but these errors were encountered: