I'm struggling to define the appropriate types for a function that produces a yup
schema for oneOf
using TypeScript enums.
The following code functions correctly and returns the type yup.MixSchema<SomeEnum>
const schema1 = yup.mixed<SomeEnum>().oneOf(Object.values(SomeEnum)); // yup.MixSchema<SomeEnum>
However, when attempting to create a generic function to return the schema, I end up with the type
yup.MixSchema<typeof SomeEnum>
. Note the presence of typeof
.
const oneOfEnum = <T>(enumObject: T) =>
yup.mixed<T>().oneOf(Object.values(enumObject));
const schema2 = oneOfEnum(SomeEnum); // yup.MixSchema<typeof SomeEnum>
https://codesandbox.io/s/yup-typescript-oneof-enum-vpvjn?file=/src/index.ts