I create string arrays using const assertions and then use them to generate union types.
const elements = ["apple", "banana", "orange"] as const;
type elementsUnion = typeof elements[number]; // type elementsUnion = "apple" | "banana" | "orange"
Is there a way to streamline this process by creating a utility type that eliminates the need to repeatedly type typeof
and [number]
? I use this pattern frequently, so it would be beneficial to find a more concise approach.