Skip to content

Discriminated unions

Posted on:
less than a minute

While writing saku-docs, a static documentation generator built with Astro.

nav: z.array(
    z.discriminatedUnion("type", [
      z.object({
        type: z.literal("docs"),
        label: z.string(),
        slug: z.string(),
        dir: z.string(),
      }),
      z.object({
        type: z.literal("blog"),
        label: z.string(),
        slug: z.string(),
        dir: z.string(),
      }),
      z.object({
        type: z.literal("openapi"),
        label: z.string(),
        slug: z.string(),
        spec: z.string().or(z.url()),
      }),
      z.object({
        type: z.literal("ghLink"),
        href: z.string(),
        label: z.string().optional(),
      }),
    ]),
  ),