#CLOSED.

25 messages · Page 1 of 1 (latest)

last patrolBOT
#
Mkoes Rzeczny#4044

Preview:ts type test = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ][number]

brittle scaffold
#

unions are sorted by declaration order

last patrolBOT
brittle scaffold
#

!ts

last patrolBOT
#
// 8<
type OutOfOrder = 11 | 21 | 16;
type Test = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23][number]
//   ^? - type Test = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23```
brittle scaffold
#

uhhh

#

in the playground it's out of order

#

seems like it broke in 4.7

sturdy jacinth
brittle scaffold
#

well

#

theres one easy way but it would be cringe af

#

just having a type _Unused = 0 | 1 | 2 | 3 | 4 | ... at the entrypoint (or at least, imported as early as possible)

#

(but not exported)

sturdy jacinth
# brittle scaffold just having a `type _Unused = 0 | 1 | 2 | 3 | 4 | ...` at the entrypoint (or at ...

It took me a while but I managed to create a workaround for this problem but that doesn't do anything either, even though it works for a while it goes back to the wrong sorting for some reason after a while I also tried to package it into an interface and somehow load it from there properly but apparently this is a bug that can't be fixed manually.

type Union_To_Spread<Object_Input> = { 
  [Key in keyof Object_Input]: Object_Input[Key] 
};

type Array_Into_Record<
  Input extends number[], 
  Result extends Record<number, never> = {}
> = Input extends [
    infer First_Number extends number, 
    ...infer Rest_Numbers extends number[]
  ] ? Array_Into_Record<
    Rest_Numbers, 
    Union_To_Spread<{ [K in keyof Result]: Result[K] } & { [K in First_Number]: never}>
  > : keyof Result;

type number_list = Array_Into_Record<[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28]>;
const valid: number_list = 25;
const invalid: number_list = 30;
brittle scaffold
#

note that packaging it into an interface will do nothing

#

it is possible to fix manually

#

see here:

last patrolBOT
#
n_n#2622

Preview:ts type Test = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 ][number] // ^? type OutOfOrder = 11 | 21 | 16

sturdy jacinth
brittle scaffold
#

it all depends on the order typescript sees the types

#

if the order is wrong it means typescript is seeing other numbers first

brittle scaffold
sturdy jacinth
sturdy jacinth
#

good however, I come to the conclusion that it is still impossible, thanks for help @brittle scaffold