Default values with type-safety
I'm using tanstack form + shadcn + zod.
I have a multi-step form and in the third step I have a "select your confirmation method" step to choose between "Email" or "Sms" radio buttons.
For my method variable I have assigned a "" (empty string") but I loose the type-safety because it should be "sms" | "email" and I got a bunch of issues in other functions due to the loose of the type.
Any workaround to tell tanstack form that a specific field is from a enum?
3 Replies
stormy-gold•10mo ago
You could add a Type Assertion to the empty string as you know that it won't pass the validations (emtpy string does not match your ENUM-Values) and you do not want a preset value for the confirmation method
continuing-cyanOP•10mo ago
Do you mean "" as MyCustomEnum ?
I havent tried, i'll give it a try man, thanks!
stormy-gold•10mo ago
There's been a short discussion about this here as well 🙂
https://github.com/TanStack/form/pull/982#discussion_r1806067561