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
Array::reverse returns generic-sized array for tuples #13360
constoriginal: [number,number]=[1,0];// Type 'number[]' is not assignable to type '[number, number]'constreversed: [number,number]=original.reverse();
Expected behavior:
original.reverse() should be considered to be [number, number].
Actual behavior:
original.reverse() is considered to be number[].
The problem is in lib.d.ts, where reverse() returns T[].
One potential fix could be to change it to return this the way sort(...) does, but that would result in the same issues with sort pointed out in 11924#issuecomment-269055091.
// Pretend `reverse()` returns `this`:constoriginal: [1,0]=[1,0];constinaccurate: [1,0]=original.reverse();// Type '[1, 0]' is not assignable to type '[0, 1]'constaccurate: [0,1]=original.reverse();
changed the title [-]Array::reverse returns generic-sized array no matter what[/-][+]Array::reverse returns generic-sized array for tuples[/+]on Jan 9, 2017
I'm not convinced this is true, it might make sense. However, once sorted, treating it as tuple definitely makes no sense. I think reverse is correct and sort is the issue.
It makes no sense to sort a tuple
I think reverse is correct and sort is the issue
I don't think it's the responsibility of the language to dictate what subset of available features its data types are used for. TypeScript in particular has the dual task of both being a real programming language and representing JavaScript in all its peculiarities. If it's possible and reasonable to do something in JavaScript, TypeScript is generally tasked with accurately reflecting that behavior.
Tuples are really just arrays of known length. It's completely reasonable that someone would both know the length of their arrays and that they want to reverse or sort them.
Aluan Haddad (@aluanhaddad) Both sort() and reverse() are problematic with tuples because of their side-effectful nature. A proper typing of these methods requires a typing rule that allows a method to change the type of its target in the scope in which it was invoked, after the point of invocation.
Josh Ghoulberg 👻 (@JoshuaKGoldberg) in the absence of the perfect type system that can express exactly what happens when you side-effectfully reverse or sort a heterogeneous array of known length (when does anyone ever do this on purpose btw?), should these operations still be available and assigned a completely erroneous type? Or is it better to err on the side of safety and lose those capabilities? All type systems disallow some programs which are perfectly correct, and the task is to find the happy medium.
Activity
[-]Array::reverse returns generic-sized array no matter what[/-][+]Array::reverse returns generic-sized array for tuples[/+]aluanhaddad commentedon Jan 9, 2017
In #11924 Tom Crockett (@pelotom) goes on to say
I'm not convinced this is true, it might make sense. However, once sorted, treating it as tuple definitely makes no sense. I think
reverseis correct andsortis the issue.JoshuaKGoldberg commentedon Jan 9, 2017
I don't think it's the responsibility of the language to dictate what subset of available features its data types are used for. TypeScript in particular has the dual task of both being a real programming language and representing JavaScript in all its peculiarities. If it's possible and reasonable to do something in JavaScript, TypeScript is generally tasked with accurately reflecting that behavior.
Tuples are really just arrays of known length. It's completely reasonable that someone would both know the length of their arrays and that they want to reverse or sort them.
pelotom commentedon Jan 9, 2017
Aluan Haddad (@aluanhaddad) Both
sort()andreverse()are problematic with tuples because of their side-effectful nature. A proper typing of these methods requires a typing rule that allows a method to change the type of its target in the scope in which it was invoked, after the point of invocation.Josh Ghoulberg 👻 (@JoshuaKGoldberg) in the absence of the perfect type system that can express exactly what happens when you side-effectfully reverse or sort a heterogeneous array of known length (when does anyone ever do this on purpose btw?), should these operations still be available and assigned a completely erroneous type? Or is it better to err on the side of safety and lose those capabilities? All type systems disallow some programs which are perfectly correct, and the task is to find the happy medium.
RyanCavanaugh commentedon Jan 9, 2017
This isn't safe. The problem is that tuples can be aliased to longer tuples:
JoshuaKGoldberg commentedon Jan 9, 2017
👍