-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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] tuple type information is lost as T[] #8276
Comments
The type information is not being lost. let coordinates = { latlng: [35, -112] }; the inferred type of the object literal is { latlng: number[]; }; not world({ latlng: [35, -112] }); it is target typed based on the function's parameter type to that more specific type. While it is not entirely clear what the type of var t = [1, 2]; // t has type number[]
var [x, y] = [1, 2]; // x and y are of type number, there is no name for the tuple itself. At any rate, neither the interface |
My point is that |
Which is why I said it's not entirely clear what the type should be. Either one will be incompatible with certain signatures or assignments. |
I suppose it could be number[] & [number, number]; But that could get out of hand pretty quickly |
This is by design. An array literal is considered a tuple only if it is contextually typed by a tuple type; for example, if it is assigned to a variable of a tuple type or passed as an argument for a parameter of a tuple type. In cases where the context provides no clear indication that it should be a tuple, we treat it as an array. It's not clear what else we would do, but we're certainly open to suggestions. |
I think the current behavior matches the syntactic intuition that follows from the respective declaration forms for tuples and arrays. |
Ok, thanks guys! |
TypeScript Version:
nightly (1.9.0-dev.20160213)
Code
Expected behavior:
Compile without errors.
Actual behavior:
Error,
[T]
tuple type information is lost asT[]
:The text was updated successfully, but these errors were encountered: