0

i want to catch a type from a union (just about any type) for example if i have type T = 'a' | 'b' | 'c'; i want to have a type called CatchRandom

so that if type R = CatchRandom<T>; R will be one of the three types 'a' or 'b' or 'c'

--> in fact i'm building this type Arr to get one array of types from a union of types (array of all types)

type CatchOne<T> = T;

type Arr<U extends string, K extends U = CatchOne<U>, R extends any[] = []> = {
    [S in U]: Exclude<U, S> extends never ? [...R, S] : TupleUnion<Exclude<U, S>, [...R, S]>;
}[K];

type UUU = Arr<'a' | 'b', 'a'>;

how can i write the type CatchOne to catch just any type from that union T so that i can use it to get a random value

basicaly i want the type UUU to be ["a", "b"] without specifying that 'a' everytime, i just pass in the union and i get the array

mod7ex
  • 874
  • 9
  • 27
  • 1
    how would that work? Determine a random type everytime you open the editor? Or everytime you hover over `R`? Or maybe just once, in that case just do `type R = 'a'` but be sure to randomly choose one beforehand. – Tobias S. Oct 22 '22 at 16:55
  • @TobiasS. once is enough, i just need to catch one , but i don't know what those types will be for example i want to catch a Interface key randomly and we can't expect how that interface will be – mod7ex Oct 22 '22 at 16:59
  • can you share a full example? This feels like an XY problem. You are probably trying something different and this is your (not working) solution. – Tobias S. Oct 22 '22 at 17:00
  • @TobiasS. actually this is what i'm building i edited that question – mod7ex Oct 22 '22 at 17:07
  • You cannot "randomly" get a type... – kelsny Oct 22 '22 at 17:09
  • *"to get one array of types in a union type"* - not sure what you mean by that. What should the type of `UUU` be? – Tobias S. Oct 22 '22 at 17:11
  • @TobiasS. basicaly i want the type UUU to be ["a", "b"] without specifying that 'a' everytime, i just pass in the union and i get the array – mod7ex Oct 22 '22 at 17:17
  • 1
    So then you want this: https://stackoverflow.com/questions/55127004/how-to-transform-union-type-to-tuple-type - seems like it randomness was never a part of this problem. – Tobias S. Oct 22 '22 at 17:19
  • @TobiasS. yes exactly anyway thanks for help , i saw that solution but it's sometimes so deep and not for practical usage – mod7ex Oct 22 '22 at 17:22

0 Answers0