I want to use a single array const fruits = ['banana', 'apple', 'orange'] as a normal array, and as a type.
I should be able to do this:
const x: fruits // => only accepts 'banana', 'apple' or 'orange'
And also be able to do this:
@IsIn(fruits)
I've tried to declare the array as <const>, such as:
const fruits = <const>['banana', 'apple', 'orange']
type Fruits = typeof fruits[number] // this evaluates to type: "banana" | "apple" | "orange"
But @IsIn(fruits) will return the following error:
Argument of type 'readonly ["banana", "apple", "orange"]' is not assignable to parameter of type 'any[]'.
The type 'readonly ["banana", "apple", "orange"]' is 'readonly' and cannot be assigned to the mutable type 'any[]'.ts(2345)
So I thought if I created two arrays, a normal one and a readonly one, it should work. So I tried this:
const fruits = ['banana', 'apple', 'orange']
const fruits_readonly: <const>[...fruits]
type Fruits = typeof fruits_readonly[number]
But now Fruits evalutes to type: string instead of type: "banana" | "apple" | "orange".