How to extract a new type alias from an object's value
const test = {
'a': ['music','bbq','shopping'],
'b': ['move','work']
};
How to get it from a test object
type Types = 'music' | 'bbq' | 'shopping' | 'move' | 'work'
You can do this quite easily using as const
:
const test = {
'a': ['music','bbq','shopping'],
'b': ['move','work']
} as const;
type Elements = typeof test[keyof typeof test][number];
The only solution I can think of that is not using as const
is defining an interface with the exact elements of the arrays:
interface Foo {
a: ['music', 'bbq', 'shopping'],
b: ['move', 'work']
};
type Elements2 = Foo[keyof Foo][number];