-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Record types should be partial #751
Comments
The current behavior matches TypeScript's |
@scotttrinh OK, but how do you feel about the inconsistency between how zod parses, and how the inferred typescript interface checks the same record? |
Ahh, I see what you mean, I missed that part of the issue in my first read. Yeah, we should align the runtime and compile time behavior, I think. @colinhacks this feels like a breaking change since it causes schemas to fail that previously succeeded. Any thoughts or suggestions about what to do here? |
@scotttrinh @colinhacks One option might be to accept and document the breaking change as a bug fix for the inconsistency (enum record keys have only been available since 3.9 and are not yet well documented, so won't be widely adopted). Then we could add a |
@scotttrinh I'm not familiar with your release process/schedule... what kind of timeframe should I expect for you making a decision on this? (If you want to go a different route, I could put together an alternative PR.) I already used zod in our app before realizing it doesn't work for our enum-keyed maps. Just trying to decide whether to stick with my patched fork for now, or look for a different workaround... Thanks! |
@colinhacks is the lead developer of Zod and is typically the one who cuts releases, so I'll defer to him on both the timing and likelihood of making this change. I think sticking with your fork is fine for now, but it's completely possible that we "fix" (or not?) it a different way, so definitely make the choice that makes the most sense for you and your project. |
Typescript has support for using partial with record. If we can add partial to z.record, it would be great. And this won’t be breaking changes. |
Just came across this, and it seems like if you use |
I love that in v3.9 we can now specify the type of the record key using enums or literals.
But the inferred
Record
type now needs to be wrapped inPartial
, otherwise typescript forces us to specify every key, which is inconsistent with how the Zod parser interprets the same schema.For example, suppose we create a schema that allows us to map states to temperatures:
If we map a couple of states, it parses fine. This parses without throwing:
But if we try to use the inferred type with the same object, typescript is not happy:
This gives us this error:
It wants us to specify all the states in the enum, which is not intuitive or useful, and is inconsistent with the Zod parser.
The text was updated successfully, but these errors were encountered: