1 | initial version |
There are several ways to determine the string literal type in an array without using 'as const':
const fruits: ['apple', 'banana', 'orange'] = ['apple', 'banana', 'orange'];
enum Fruit {
Apple = 'apple',
Banana = 'banana',
Orange = 'orange'
}
const fruits: Fruit[] = [Fruit.Apple, Fruit.Banana, Fruit.Orange];
type Fruit = 'apple' | 'banana' | 'orange';
const fruits: Fruit[] = ['apple', 'banana', 'orange'];
All these methods ensure that the array can only contain the specific string literal types defined and not any other arbitrary string value.