I like the tricks :) – Kasia
Going from tuple to union is easy but sometimes you need the opposite.
type TuplifyUnion<U extends string, R extends any[] = []> = {
[S in U]: // for each variant in the union
Exclude<U, S> extends never // remove it and..
? [...R, S] // ..stop recursion if it was the last variant
: TuplifyUnion<Exclude<U, S>, [...R, S]> // ..recur if not
}[U] // extract all values from the object
type Screen = "Home" | "Login"
type AllScreens = TuplifyUnion<Screen>
const allScreens: AllScreens = ["Home"] // ✅ Does not compile
Crafted with 💛 by Riccardo
Crafted with 💛 by Riccardo