i have a function which takes two parameters one is a key and other is an enum type. I am trying to generalize this function with strong typing to take any enum type and return the value:
export const getStatus = <T>(key: string, statEnum: any): string => {
return statEnum(key);
}
function call:
console.log(getStatus<SVC_A>('AUTH_122', SVC_A));
This above function works with paramater typed as any statEnum: any
but if I change it to statEnum: typeof T
it throws an error message
T only refers to a type, but is being used as a value here
Parameter typing works if I add in a variable
type supportedEnums = typeof SVC_A | typeof SVC_B
export const getStatus = <T>(key: string, statEnum: supportedEnums): string => {
return statEnum(key);
}
curious to understand, why it works with supportedEnums type but when it's added as typeof T
it doesn't.
i also tried it as
export const getStatus = <T>(key: string, statEnum: T): string => {
return statEnum(key);
}
updated function parameter type to T statEnum: T
it thorws error message while calling the function
function call:
console.log(getStatus<SVC_A>('AUTH_122', SVC_A));
Argument of type 'typeof SVC_A' is not assignable to parameter of type 'SVC_A'
Is there a possibility to define an enum as a parameter and typesafe?