superstruct
superstruct copied to clipboard
Native Typescript enum support
Hello, is there a way to express that some struct field must be of a given TS enum type ?
enum MyEnum {
Value1 = 'val1',
Value2 = 'val2',
}
const MyStruct = object({
// Something like this:
myEnum: nativeEnum(MyEnum)
})
// Here the MyStruct type would have a myEnum key of type MyEnum
type MyStruct = Infer<typeof MyStruct>;
Thanks
So for a string enum, one can do:
const MyStruct = object({
myEnum: enums(Object.values(MyEnum))
})
But for numeric enums it does not work
@htulipe this is an interesting idea. I'd be open to a PR that supported native enum-shaped objects as the config parameter for enums.
any update on that? 🙏