javascripttypescriptunion-typesfp-tsio-ts-library

How to transform a JS array of strings into a union with io-ts?


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"),
);

Solution

  • 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))