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

Tuples constructed via array literals don't typecheck against tuple constraints #3369

Closed
ccapndave opened this issue Jun 3, 2015 · 8 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@ccapndave
Copy link

type Point = [number, number]; var x = [1, 2]; var p: Point = x;

In this case p won't typecheck because x has been inferred as number[] instead of [number, number]. It would be really nice if the type system could check against both types.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 3, 2015

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 mhegazy added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jun 3, 2015
@mhegazy mhegazy closed this as completed Jun 3, 2015
@Arnavion
Copy link
Contributor

Arnavion commented Jun 3, 2015

@mhegazy Since tuple types decay to the corresponding array type, will it really be a breaking change?

@DanielRosenwasser
Copy link
Member

@Arnavion what about the following case?

var x = [1, 2];
x = [1]

@mhegazy
Copy link
Contributor

mhegazy commented Jun 3, 2015

tuples are more restrictive than arrays, since they have a size constraint. just like in @DanielRosenwasser example.

@Arnavion
Copy link
Contributor

Arnavion commented Jun 3, 2015

Ah, right.

@ccapndave
Copy link
Author

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).

@mhegazy
Copy link
Contributor

mhegazy commented Jun 3, 2015

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.

@FranklinWhale
Copy link

If the length of an array is known and its type matches with a tuple declaration, should the assignment be allowed?

See also #6574 and #7799

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

5 participants