You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
feature: Implement type guard in the isEnum() function #2300
The isEnum() function does not implement type guard.
This is inconvenient when writing code like the following
constsomefunction(value: string): USERTYPE=>{if(!isEnum(value,USERTYPE))thrownewError("not match enum");returnvalue;// Type 'string' is not assignable to type 'USERTYPE'. ts(2322)}
Proposed solution
A type guard should be implemented in the isEnum() function.
I am now wrapping the isEnum() function in the type guard function myself.
functionvalidateEnumValue<T>(value: any,enumeration: T): value is T[keyofT]{returnisEnum(value,enumulation);}
Specifically, I believe this can be accomplished by modifying the code as follows.
exportfunctionisEnum<TextendsRecord<string|number,unknown>>(value: unknown,entity: T): value is T[keyofT]{constenumValues=Object.values(entity);returnenumValues.includes(value);}
Activity
kterui9019 commentedon Nov 3, 2023
Specifically, I believe this can be accomplished by modifying the code as follows.
I can send you a pull request if you like.
braaar commentedon Jan 11, 2024
I'm moving this to the discussion tab, as we are trying to keep the issue count low. Please see this issue for an explanation.