-
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
Cloning a tuple turns it into an array #16503
Comments
I suppose we can make Tuple extend from a new |
For some reason #4988 was closed but this was left open. I just want one correct, non-verbose way to clone a tuple. |
I don't remember exactly what led me to post this a few months ago but as of now it looks like |
I don't remember the circumstances when I posted that, but it looks like I was probably wrong. I spent a minute trying different versions of the playground and all the ones I can find behave the same way -- |
Spread works now, which seems sufficient + idiomatic, and I think we don't have any good mechanisms to hardcode |
Thanks Ryan, is this in the docs anywhere? I can't find anything about spread on the Tuple page, or anything about tuples on the Spread page. I'm trying to understand how implied types work with spread, specifically. In this example, you can explicitly type an assignment from a spread-clone without any errors, but if you don't give any hints, it still winds up as a non-tuple array. I'd still like to have a built-in that copies a tuple and always treats the result as the same tuple signature; I guess I could write a lightweight wrapper that just uses spread if I have to. |
This is a facet of contextual typing: array literals get checked as tuples if contextually typed by a tuple type. |
TypeScript Version: every version tested (from 2.3.0 to 2.5.0-dev.20170613)
Code
Expected behavior:
No error
Actual behavior:
array.slice()
andarray.slice(0)
and[...array]
are common idiomatic ways to shallow-clone an array. Unfortunately, this loses the tuple type information.I don't know how easy this would be to fix, but even something like supporting only tuples below a certain length (by hardcoding
[T1, T2] => [T1, T2]
type signatures forArray#slice
) would be a pretty big improvement for my own use-case.The text was updated successfully, but these errors were encountered: