Skip to content

Type [number,number] is not recognized correctly #12579

Closed

Description

@k8w

TypeScript Version: 2.1.1 / nightly (2.2.0-dev.201xxxxx)
2.1.1

Code

type Data = [number, number];
let a:Data = [1,2,3,4,5].map(v=>[v, v+1]);

Expected behavior:
Compiled successfully.

Actual behavior:

test.ts(3,5): error TS2322: Type 'number[][]' is not assignable to type '[number, number]'.
Property '0' is missing in type 'number[][]'.

It seems work well in 2.0, throw this error after update to 2.1

Activity

  1. aluanhaddad commented on Nov 30, 2016

    @aluanhaddad
    Contributor

    Error message is a bit off putting but I think the behavior is right.
    You can add this to make it work

    interface Array<T> {
      map(f: (x: T) => [T, T]): [T, T][];
    }
    
    type Data = [number, number];
    let a: Data[] = [1, 2, 3, 4, 5].map(v => [v, v + 1]);
  2. k8w commented on Nov 30, 2016

    @k8w
    Author

    Shouldn't v=>[v, v+1] be recognized as [number, number] instead of number[][] ?

  3. mhegazy commented on Nov 30, 2016

    @mhegazy
    Contributor

    Tuple types are only inferred in the presence of a contextual type. e.g. let a: Data = [1, 2]. the issue here is that the contextual type does not flow through map, since the return type position is not a generic argument inference site. issue #11152 is tracking fixing that.

  4. mhegazy commented on Nov 30, 2016

    @mhegazy
    Contributor

    closing in favor of #11152

  5. locked and limited conversation to collaborators on Jun 19, 2018
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

    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