I have a basic enum below:
export enum Fruit {
apple,
banana
}
I want to export a fixed Array of the enum keys
export const Fruits = Object.entries(Fruit).map(f => f[0]);
Which should give, as desired, ['apple', 'banana'] and Fruits typed as string[].
In an attempt for a more specific typing, I added as keyof typeof Fruit like so
export const Fruits = Object.entries(Fruit).map(f => f[0] as keyof typeof Fruit);
Which gives me the type const Fruits: ("apple" | "banana")[]. Is this the most that I can get?
I was aiming to get a typing like const Fruits: ["apple", "banana"] = .... which I think is the perfect typing I should produce.
Footnote:
I wouldn't like to utilize the other method of defining enums, Just for the sake of avoiding that redundancy,
export enum Fruit {
apple = 'apple',
banana = 'banana'
}
And I am happy to do something like:
interface Meal {
fruit: keyof tpeof Fruit // since the default enum values are integers, use keys
}
So I'd be happy to have a solution that doesn't require me to do so. If there's no other way, do mention in your answer.