Skip to content

Soundness issue with Array.prototype.concat and arrays of arrays #19535

Closed
@andyfriesen

Description

@andyfriesen

TypeScript Version: 2.7.0-dev.201xxxxx

Code

const foo: [number, string][] = [[1, 'one']];
console.log(foo.concat([2, 'two']));

Expected behavior:
Either [[1, 'one'], [2, 'two']] or a type error.

Actual behavior:
[[1, 'one'], 2, 'two']

Activity

Ghabriel

Ghabriel commented on Oct 27, 2017

@Ghabriel

a.concat(b) pushes the elements of b to a, so the actual behavior is correct (also, what the functions do at runtime is JavaScript-world, not TypeScript). Your expected behavior happens if you use push instead or add extra brackets around [2, 'two']:

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']]));
andyfriesen

andyfriesen commented on Oct 27, 2017

@andyfriesen
Author

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.

mhegazy

mhegazy commented on Jul 16, 2018

@mhegazy
Contributor

One option is to change the definition of concat to be:

concat<U>(...items: (T | ConcatArray<T>)[]): T extends ConcatArray<infer E> ? (E | T)[] : T[];
added
BugA bug in TypeScript
Domain: lib.d.tsThe issue relates to the different libraries shipped with TypeScript
on Jul 16, 2018
added this to the Community milestone on Jul 16, 2018
mhegazy

mhegazy commented on Jul 16, 2018

@mhegazy
Contributor

PRs welcomed

Ghabriel

Ghabriel commented on Jul 17, 2018

@Ghabriel

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 Argument of type 'T[]' is not assignable to parameter of type 'ReadonlyArray<T>'. This seems to be a bug, or am I overlooking something @mhegazy?

mhegazy

mhegazy commented on Jul 17, 2018

@mhegazy
Contributor

did you change the declaration of both Array and ReadonlyArray?

andersk

andersk commented on Jan 26, 2019

@andersk
Contributor

Another example:

const a: number[][] = [[1]].concat([[2]], [3])
// → [[1], [2], 3]

and a related issue with flatMap:

const b: number[][] = [true, false].flatMap<number[]>(x => x ? [1] : [[2]]);
// → [1, [2]]
RyanCavanaugh

RyanCavanaugh commented on Jan 31, 2020

@RyanCavanaugh
Member

Rolling up into #36554

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: lib.d.tsThe issue relates to the different libraries shipped with TypeScriptHelp WantedYou can do this

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

      Participants

      @andersk@andyfriesen@Ghabriel@RyanCavanaugh@mhegazy

      Issue actions

        Soundness issue with Array.prototype.concat and arrays of arrays · Issue #19535 · microsoft/TypeScript