I have an interface with different keys that all accept the same enum values. I have defined a type these enum values called bloodType. Can I somehow skip defining the interface and simply state that all object keys must match the type when I define my object?
/** Defining types that will go into the interface */
type bloodType = {
| "typeA"
| "typeB"
| "typeO"
};
/** Defining the interface */
interface IPopulation {
"Bobby": bloodType,
"Jimmy": bloodType,
"Sandra": bloodType,
"Angel": bloodType,
"JimmyJohn": bloodType,
}
/** The actual object using the interface */
const population:IPopulation = {
"Bobby": "typeA",
"Jimmy": "typeB",
"Sandra": "typeB",
"Angel": "typeO",
"JimmyJohn": "typeA",
}
can I somehow skip defining the interface and just do...
type bloodType = {
| "typeA"
| "typeB"
| "typeO"
};
/** Is there some sort of shortcut like this */
const population = {
"Bobby": "typeA",
"Jimmy": "typeB",
"Sandra": "typeB",
"Angel": "typeO",
"JimmyJohn": "typeA",
}: {string: bloodType || {}}; // <- this obv doesn't work
CodePudding user response:
I think what you are looking for is the following:
type BloodType = 'typeA' | 'typeB' | 'typeO';
interface IPopulation {
[key: string]: BloodType;
}