Closed mborysenko closed 1 year ago
Is it correct way to define enum while describing type?
Hi @mborysenko -- yes, this is the correct way to define enums.
Unfortunately, as you have discovered, the Describe
helper doesn't seem to be returning the right kind of struct based on the supplied type. (If you simplify, even const redirectType: Describe<RedirectType> = enums([RedirectType.PERMANENT_REDIRECT, RedirectType.TEMPORARY_REDIRECT])
doesn't work as it should.) My hunch is this is to do with how TypeScript mixes structural and nominal typing and the difficulty in establishing which way to go when recursively Describe
-ing an object. I am not the author or maintainer of Superstruct however, so this could be totally wrong.
For what is worth, I have found that going the other way (using Infer
from a struct, instead of using Describe
from a type) is the better way for Superstruct.
export type RedirectDescriptor = Infer<typeof redirectDescriptor>
const redirectDescriptor = object({
type: enums([RedirectType.PERMANENT_REDIRECT, RedirectType.TEMPORARY_REDIRECT]),
to: string(),
});
Hope this help! ✌️
I have such code, and it doesn't compile.
The error is: