Skip to content
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

“T[][] is not assignable to Iterable<[T, T]>” while it is (at compile time) #7799

Closed
msklvsk opened this issue Apr 4, 2016 · 7 comments
Labels
Duplicate An existing issue was already created

Comments

@msklvsk
Copy link

msklvsk commented Apr 4, 2016

TypeScript Version:

1.8.9

Code

new Map<number, number>([1, 2].map(x => [x, x]));

// Error:
// Argument of type 'number[][]' is not assignable to parameter of type 'Iterable<[number, number]>'…

Expected behavior: types are inferred correctly, no error shown.
Actual behavior: type error.

@FranklinWhale
Copy link

I think this issue is related to #6574

@msklvsk
Copy link
Author

msklvsk commented Apr 4, 2016

Thanks, yes, same problem, a dupe. Should I close this one?

@Arnavion
Copy link
Contributor

Arnavion commented Apr 4, 2016

It would be nice if contextual types allowed to infer an array as a tuple, to support such an idiom.

@FranklinWhale I don't think #6574 is related. If #6574 was fixed, the inferred type would be Iterable<number[]> which would still be a problem. This issue is about inferring [x, x] as a [number, number] instead of number[].

@FranklinWhale
Copy link

I don't mean that this issue is a duplicate of #6574, but they are both related to array to tuple inference. I am sorry if any confusion has been caused.

To be clear:
In #6474, T[] may be a tuple of T.
In this issue, T[][] may be an array of tuples of T.

@Arnavion
Copy link
Contributor

Arnavion commented Apr 4, 2016

#6574 is about inferring Tuple<T>.map<U>()'s result as a Tuple<U> instead of Array<U>. It says nothing about inferring array literals as tuples (notice that it starts with explicit annotation for the tuple), whereas this issue is about inferring array literals as tuples. I.e., if [x, x] was inferred as a [number, number] instead of number[] then the code in the OP would compile.

Hence my point that they're not related.

@FranklinWhale
Copy link

That's not really the case. In @elbarzil's code sample in #6574, [1,2] in [1,2].map(x=>x+1) is actually a number[], and the map function also returns a number[]. It is actually the same as assigning an array created from an array literal to a varible declared as a tuple. Therefore, #6574 will be fixed when any array may be inferred as tuple when there is an matching tuple declaration.

@FranklinWhale
Copy link

See also #3369

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants