I know that we can extract type possibilies using an array:
const fruits = ['Apple', 'Pear', 'Banana'] as const
type fruitType = typeof fruits[number] // "Apple" | "Pear" | "Banana"
I wonder if we can do this using object indexes instead?
const fruits = {
apple: { shape: 'round', color: 'red' },
pear: { shape: 'pear-like', color: 'yellow' },
banana: { shape: 'banana-shaped', color: 'banana-colored' }
}
type fruitType = typeof indexes of fruits // "apple" | "pear" | "banana"
Thanks!
CodePudding user response:
I believe this is what you are looking for:
type fruitType = keyof typeof fruits // "apple" | "pear" | "banana";
Here is a link to the documentation as @jcalz suggested, and the Playground