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
typeInitial=[1,2,3,4]typeFindReplace<Textendsany[],Find,Replace>={[KinkeyofT]: T[K]extendsFind ? Replace : T[K]}// Map over array and replace '2'typeResultArray=FindReplace<Initial,2,'was 2'>// => [1, "was 2", 3, 4]// Now accessing the array values using [number], the type// order is lost. This matters because now when the union is later// cast as a intersection, the types are in the wrong ordertypeResult=ResultArray[number]// => 1 | 3 | 4 | "was 2
Expected behavior:
Mapping over types should preserve the key order
Actual behavior:
Mapping over types preserves the "type", but the key order is lost when performing T[keyof T] operator
Related Issues:
The text was updated successfully, but these errors were encountered:
Union order is not something that you should count on for anything. @RyanCavanaugh mentioned this when unions to tuples were discusses here:
First, union order is not something we can ever allow to be observable. Internally, unions are stored as a sorted list of types (this is the only efficient way to quickly determine relationships between them), and the sort key is an internal ID that generated incrementally. The practical upshot of this is that two extremely similar programs can generate vastly different union orderings, and the same union observed in a language service context might have a different ordering than when observed in a commandline context, because the order in which types are created is simply the order in which they are checked.
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms:
Mapped types key order lost
Code
Expected behavior:
Mapping over types should preserve the key order
Actual behavior:
Mapping over types preserves the "type", but the key order is lost when performing
T[keyof T]
operatorRelated Issues:
The text was updated successfully, but these errors were encountered: