You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Tuples should be clonable with Array.prototype.slice() #4988
So the right thing is to do what Daniel Rosenwasser (@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.
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:
interface TupleArray<T> extends Array<T> { 0: any }
The problem with this is that 0's type in TupleArray is any. If it's T, then KeyValuePair's must have a property 0: K | V, even though it should be 0: K. I think any is OK here because it represents what's going on with Array -- it's not a homogenous array anymore.
Assuming you meant slice(): TupleArray<T> that doesn't work because you lose the other numeric properties that make the tuple useful (i.e. 1, 2, etc.). You'd just end up with a type that has a property 0 whose type is the union of all types in the original tuple.
Activity
DanielRosenwasser commentedon Sep 27, 2015
Great idea - after #4910 goes in we can add an overload in lib.d.ts so that
slicewith no arguments returns thethistype.ahejlsberg commentedon Sep 27, 2015
Just added the necessary changes to #4910. When it goes in, all we need is the extra overload in lib.d.ts.
DanielRosenwasser commentedon Oct 3, 2015
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
.slice().mhegazy commentedon Oct 3, 2015
I would not say Subclassing array is the common scenario.
6 remaining items
mhegazy commentedon Oct 7, 2015
So the right thing is to do what Daniel Rosenwasser (@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.
sandersn commentedon Oct 7, 2015
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.
sandersn commentedon Oct 8, 2015
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
any. If it'sT, thenKeyValuePair's must have a property0: K | V, even though it should be0: K. I thinkanyis OK here because it represents what's going on with Array -- it's not a homogenous array anymore.Now we can take Daniel Rosenwasser (@DanielRosenwasser) 's proposal and put
sliceinsideTupleArray. But to avoid promising thatslicereturns properties from tuple subclasses, per Anders Hejlsberg (@ahejlsberg), we can actually make it returnTupleArray<T>:The only hole here is that I don't know whether this satisfies Mohamed Hegazy (@mhegazy) 's constraint that users not update their lib.d.ts. Thoughts?
DanielRosenwasser commentedon Oct 17, 2015
Assuming you meant
slice(): TupleArray<T>that doesn't work because you lose the other numeric properties that make the tuple useful (i.e.1,2, etc.). You'd just end up with a type that has a property0whose type is the union of all types in the original tuple.sandersn commentedon Nov 11, 2015
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:
...although I'm still not sure how to relate
Tand...Uhere.Igorbek commentedon Sep 21, 2016
You would not (
Tis a union of constituent types in...U), unless tuples get first-class support like here #6229