Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

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

Closed
k8w opened this issue Nov 30, 2016 · 4 comments
Closed

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

k8w opened this issue Nov 30, 2016 · 4 comments
Labels
Duplicate An existing issue was already created

Comments

@k8w
Copy link

k8w commented Nov 30, 2016

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

@aluanhaddad
Copy link
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]);

@k8w
Copy link
Author

k8w commented Nov 30, 2016

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

@mhegazy
Copy link
Contributor

mhegazy commented Nov 30, 2016

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.

@mhegazy
Copy link
Contributor

mhegazy commented Nov 30, 2016

closing in favor of #11152

@mhegazy mhegazy closed this as completed Nov 30, 2016
@mhegazy mhegazy added the Duplicate An existing issue was already created label Nov 30, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants