E
E
Ezrayter2020-10-13 21:00:37
typescript
Ezrayter, 2020-10-13 21:00:37

How to make type from array elements in typescript?

const arr = ['a', 'b', 'c'];
type MyType = 'a' | 'b' | 'c';


Is there a way to create MyType via arr ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2020-10-13
@Ezrayter

const arr = ['a', 'b', 'c'] as const;
type MyType = (typeof arr)[number];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question