I have the following method which transforms a string instance of an enum to the corresponding enum member on an object:
function transformEnum<TBase>(base: TBase, member: keyof TBase, enumInstance: any) {
base[member] = enumInstance[base[member]];
}
It's called like this:
transformEnum(result, "day", DayOfWeek);
Is there any way to type the enumInstance
variable? The typing doesn't have to be perfect, but constraining it at least somewhat would be nice.
Alternatively I've tried
function transform<T>(get: () => T, set: (x: T) => void, enumInstance: T) {
set((enumInstance as any)[get()]);
}
But when I call this like so:
transform<DayOfWeek>(() => result.day, (x) => { result.day = x; }, DayOfWeek);
I get
[ts] Argument of type 'typeof DayOfWeek' is not assignable to parameter of type 'DayOfWeek'.