6

I'm using io-ts and i'm wondering if there's a way to turn an array of strings (literals) into a union of such literals. For example:

export const CONTROLS = [
  "section",
  "text",
  "richtext",
  "number",
];

export const ControlType = t.union(
  // What to do here? Is this even possible? This is what came to mind but it's obviously wrong.
  // CONTROL_TYPES.map((type: string) => t.literal(type))
);

I don't know if this is possible but given that io-ts is just JS functions I don't see why not. I just don't know how.

The end result in this case should be (with io-ts):

export const ControlType = t.union(
  t.literal("section"),
  t.literal("text"),
  t.literal("richtext"),
  t.literal("number"),
);
Obed Parlapiano
  • 3,226
  • 3
  • 21
  • 39

1 Answers1

8

io-ts formally recommends to use keyof for better performance with string literal unions. Thankfully, that also makes this problem much easier to solve:

export const CONTROLS = [
    "section",
    "text",
    "richtext",
    "number",
] as const;

function keyObject<T extends readonly string[]>(arr: T): { [K in T[number]]: null } {
    return Object.fromEntries(arr.map(v => [v, null])) as any
}

const ControlType = t.keyof(keyObject(CONTROLS))
Rubydesic
  • 3,386
  • 12
  • 27
  • This is what the code above for ControlType generates: ` t.KeyofC<{number: null, section: null, text: null, richtext: null}>`. Which is correct since it'll only get `keyOf` – Obed Parlapiano Jul 20 '21 at 15:00