T
TanStack•13mo ago
rare-sapphire

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
flat-fuchsia
flat-fuchsia•13mo 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
rare-sapphire
rare-sapphireOP•13mo ago
Do you mean "" as MyCustomEnum ? I havent tried, i'll give it a try man, thanks!
flat-fuchsia
flat-fuchsia•13mo ago
There's been a short discussion about this here as well 🙂 https://github.com/TanStack/form/pull/982#discussion_r1806067561

Did you find this page helpful?