-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Closed
Description
Problem
I want to restrict options passed to z.enum
to the existing type.
type Status = 'new' | 'accepted' | 'closed'
z.enum(['new', 'accepted']) // OK
z.enum(['new', 'approved']) // Invalid choice - should be somehow disallowed
Is there an elegant way to statically validate the typings of enum options?
Solutions tried
z.enum(['new', 'accepted'] as Status[]) // Argument of type 'Status[]' is not assignable to parameter of type '[string, ...string[]]'.
z.enum(['new', 'accepted'] satisfies Status[]) // Argument of type '("new" | "acepted")[]' is not assignable to parameter of type '[string, ...string[]]'.
z.enum<Status>(['new', 'accepted']) // Expected 2 type arguments, but got 1
z.enum(['new', 'accepted'] as [Status, ...Status[]]) // works, but ugly
z.enum(['new', 'accepted'] satisfies [Status, ...Status[]]) // works, but ugly
z.enum(['new', 'accepted'] as const satisfies readonly Status[]) // works, but could it be simpler?
at least a built-in helper would be useful:
z.enum(['new', 'accepted'] as z.EnumOptions<Status>) // self explanatory
acidoxee, josuazurbruegg and kpkonghk01JacobWeisenburger
Metadata
Metadata
Assignees
Labels
No labels