Skip to content

feature: Implement type guard in the isEnum() function #2300

Description

@kterui9019

Description

The isEnum() function does not implement type guard.
This is inconvenient when writing code like the following

const somefunction(value: string): USERTYPE => {
  if (!isEnum(value, USERTYPE)) throw new Error("not match enum");
  return value; // 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.

function validateEnumValue<T>(value: any, enumeration: T): value is T[keyof T] {
  return isEnum(value, enumulation);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    flag: needs discussionIssues which needs discussion before implementation.type: featureIssues related to new features.

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions