-
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 should be clonable with Array.prototype.slice() #4988
Comments
Great idea - after #4910 goes in we can add an overload in lib.d.ts so that |
Just added the necessary changes to #4910. When it goes in, all we need is the extra overload in lib.d.ts. |
Actually, I'm not so sure about this anymore; if you subclass array, then you definitely aren't going to get the expected behavior when you |
I would not say Subclassing array is the common scenario. |
In our compiler we have |
If we used an alternate interface instead of Array, then at least this would be slightly less deceptive. Something like interface TupleArray<T> extends Array<T> {
slice(): this;
} |
As far as I understand, the deception (pun?) is built in to the re-purposing of arrays as tuples. This PR just makes the pun applicable in more places. I don't think the result of |
@sandersn The issue is that the implementation of If we change |
It's also worth stating that if a user is totally sure that this is what they want, they can always add the overload themselves. |
So the right thing is to do what @DanielRosenwasser suggested and have a new type to represent the Tuple that extends Array. one thing to note, we would not want to require users to update their library as they update the compiler (as some users have a hand crafted version of the library) so we need to come up with a solution that does not violate this constraint. |
I need to go read the spec for tuple types and assignment. Right now I don't understand well enough how the type system supports it. I'll see if I can come up with a proposal afterwards. |
After looking at the spec, the relevant section is 3.3.3. It gives an example of a named tuple type:
It also says that a type is said to be a tuple-like type if it has a property with the numeric name '0'. Combining these two, I guess we could change the spec to include an explicit tuple type that is the parent of all tuple types:
Then KeyValuePair becomes
The problem with this is that 0's type in TupleArray is Now we can take @DanielRosenwasser 's proposal and put interface TupleArray<T> extends Array<T> {
0: any;
slice: TupleArray<T>;
} The only hole here is that I don't know whether this satisfies @mhegazy 's constraint that users not update their lib.d.ts. Thoughts? |
Assuming you meant |
Closing for now -- we should revisit once we have variadic kinds for tuple types so that there is at least a chance to write a type like: interface Array<T> {
slice<...U>(): ...U;
} ...although I'm still not sure how to relate |
You would not ( |
Let's say i define a tuple type like this :
Now when i do this:
The compiler complains that
TS2322: Type 'number[]' is not assignable to type '[number, number]'. Property '0' is missing in type 'number[]'.
I think it should work, slice without argument (or slice(0) or even slice(0,2)) should allow to clone a tuple.
The text was updated successfully, but these errors were encountered: