Array containing all options of type value in Typescript

If you define the array first, here’s a way to get the Fruit type

const arrayWithAllFruitsAsvalues = ['apple', 'peach'] as const;

type Fruit = typeof arrayWithAllFruitsAsvalues[number];  // "apple" | "peach"

const objectWithAllFruitsAsKeys: {
  [ key in Fruit ]: any
} = { apple: '', peach: '' }

Leave a Comment