Skip to content
Nate
Stephens

Define a Union Type from an Array

Can be an array of anything. Just need to use an as const assertion on the array.

const themeOptions = [
  {
    label: 'System',
    value: 'system',
    icon: OSIcon,
  },
  {
    label: 'Light',
    value: 'light',
    icon: SunIcon,
  },
  {
    label: 'Dark',
    value: 'dark',
    icon: MoonIcon,
  },
] as const;

type ThemeOption = (typeof themeOptions)[number];

// type ThemeOption = {
//     readonly label: "System";
//     readonly value: "system";
//     readonly icon: any;
// } | {
//     readonly label: "Light";
//     readonly value: "light";
//     readonly icon: any;
// } | {
//     readonly label: "Dark";
//     readonly value: "dark";
//     readonly icon: any;
// }

Last Updated: