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.
Asked
Active
Viewed 21 times
0
masinnlos
- 117
- 8
1 Answers
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;
Mr. Polywhirl
- 42,981
- 12
- 84
- 132