-
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
Tuples constructed via array literals don't typecheck against tuple constraints #3369
Comments
Tuple types are not inferred from arrays, they have to be specified explicitly. so in your example: var x: [number, number] = [1, 2]; or the type alias way as you specified. Changing how an array literal type being inferred would be a huge breaking change that would not be feasible at this point; also it is not clear if this would be more helpful than inferring an array in the general case. |
@mhegazy Since tuple types decay to the corresponding array type, will it really be a breaking change? |
@Arnavion what about the following case? var x = [1, 2];
x = [1] |
tuples are more restrictive than arrays, since they have a size constraint. just like in @DanielRosenwasser example. |
Ah, right. |
I suppose at this point it could fall back to the least restrictive type, but I do see that its going to be pretty complicated (and maybe a bit unpredictable). |
The problem is there is not really a fall back.. the type is either an array or a tuple.. inferring a tuple would enable some scenarios but would break others and vice versa. |
In this case
p
won't typecheck because x has been inferred asnumber[]
instead of[number, number]
. It would be really nice if the type system could check against both types.The text was updated successfully, but these errors were encountered: