I have been working on a method to verify if a value retrieved from the database matches an enum. At first, I aimed to create a generic TypeGuard, but then I thought it would be beneficial to have a function that not only checks the value against the enum but also handles both scenarios. Unfortunately, TypeScript does not allow using `
typeGuard<E extends Object>(value: string): value is E {
return Object.values(E).includes(value);
}
Upon realizing that passing E to Object.values() is invalid since E is a type, not an object, I encountered a roadblock. One workaround could be passing the object as another argument, but that seems cumbersome as I won't be utilizing the object in any other way. The same issue arises if I only need it as a function, not a type guard.
typeGuard<E extends Object>(values: string): boolean {
return Object.keys(E).includes()
}
In summary, I am seeking a method to validate if a given value corresponds to a value within an object solely based on the object's type, without referencing the object itself. Is there a solution to achieve this?