I have a specific Enum:
enum MyEnum {
optionOne = 0,
optionTwo = 1,
optionThree = 2,
optionFour = 3,
}
and a related Type:
export type EnumNamesList = keyof typeof MyEnum;
I am looking to create a type similar to this:
export type EnumDataTypes = {
optionOne: string;
optionTwo: boolean;
optionThree: Date
}
while making sure that all property names are of type EnumNamesList.
Any ideas or suggestions on how to achieve this?
I attempted the following approach:
export interface EnumData extends Record<EnumNamesList, any> {
optionOne: string;
optionTwo: boolean;
optionThree: Date
}
however, this code results in a compilation error:
export interface EnumData extends Record<string, Date> {
optionOne: string;
}
this one does not throw an error:
export interface EnumDataExample extends Record<EnumNamesList, any> {
notValid: string;
}
The main objective is to have a class structured like so:
class MyClass<TypeDefinitions extends Partial<Record<EnumNamesList, any>>>{
fetchValue<T extends EnumNamesList>(name: T): TypeDefinitions[T]{
return {} as any;
}
}
where a defined type is passed to specify the return types of the fetchValue function. It should only be feasible to define recognized property values within that type while still allowing for partial types to be passed through.