0

I want to define an array with a constant value e.g. const arr = ["a", "b"];. However, the compiler will think it's string[]. Is there is a way to make it a constant type as is so the type of arr should be ["a", "b"]. The only way I found was to do like: const arr: ["a", "b"] = ["a", "b"];. but it's like code duplication.

0

1 Answer 1

1

You could define it as a read-only constant. Keep in mind that a constant can never be reassigned anyways.

const CHOICES = ['a', 'b'] as const;
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.