0
export const files = ['a', 'b', 'c']

export type File = 'a' | 'b' | 'c'

How do I declare this File type without repeating the values like 'a', 'b', 'c'.

mickl
  • 48,568
  • 9
  • 60
  • 89
eguneys
  • 6,028
  • 7
  • 31
  • 63
  • 2
    An uncanny duplicate: [Derive union type from tuple/array values](https://stackoverflow.com/questions/45251664/derive-union-type-from-tuple-array-values) – kelsny Oct 07 '22 at 14:18

1 Answers1

1

You can use the typeof with number as index syntax. Note that as const is needed in your array definition:

export const files = ['a', 'b', 'c'] as const;
export type File = typeof files[number];

Typescript Playground

mickl
  • 48,568
  • 9
  • 60
  • 89