Skip to content

Typescript infer the array type incorrectly #26115

Closed
@tomkunzemann

Description

@tomkunzemann

TypeScript Version: 3.0.1

Search Terms:
type, infer, bug, map, typings, array

Code

let a: Map<string, Map<string, any>>;

const arr = [
  ['outer1', [['inner11', {}], ['inner12', {}]]],
  ['outer2', [['inner21', {}], ['inner22', {}]]],
];

const arr2: Array<[string, Map<string, any>]> = arr.map(([out, a]) => [out, new Map(a)]);

a = new Map(arr2);

Expected behavior:

Typescript should use only the following type for parameter a

{}[][]

Actual behavior:

Typescript infer the type of parameter a to

string | {}[][]

Playground Link:

https://www.typescriptlang.org/play/#src=let%20a%3A%20Map%3Cstring%2C%20Map%3Cstring%2C%20any%3E%3E%3B%0D%0A%0D%0Aconst%20arr%20%3D%20%5B%0D%0A%20%20%5B'outer1'%2C%20%5B%5B'inner11'%2C%20%7B%7D%5D%2C%20%5B'inner12'%2C%20%7B%7D%5D%5D%5D%2C%0D%0A%20%20%5B'outer2'%2C%20%5B%5B'inner21'%2C%20%7B%7D%5D%2C%20%5B'inner22'%2C%20%7B%7D%5D%5D%5D%2C%0D%0A%5D%3B%0D%0A%0D%0Aconst%20arr2%3A%20Array%3C%5Bstring%2C%20Map%3Cstring%2C%20any%3E%5D%3E%20%3D%20arr.map((%5Bout%2C%20a%5D)%20%3D%3E%20%5Bout%2C%20new%20Map(a)%5D)%3B%0D%0A%0D%0Aa%20%3D%20new%20Map(arr2)%3B%0D%0A

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions