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'.

1

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.