-
-
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
Support exactOptionalPropertyTypes
for partial
and optional
helpers
#1510
Comments
There was a discussion regarding this feature and it was silently closed. Regardless of people confirming its still a relevant issue for them (stale bot check ignored that fact). From what I'v observed so far, I doubt they will do anything. Now any Record that doesn't allow explicit undefined assignment with |
Apologies for this being something that Stale bot closed, I absolutely think we should work with Most likely, we'll introduce some new utility methods that have the new API in mind. PRs welcome! |
exactOptionalPropertyTypes
for partial
and optional
helpers
Maybe there is some misunderstanding, but the change that was introduced to record is actually a breaking change. Before the change to record, it was not possible to utilize the benefits of What the change to record does not provide is the replication of I'm not sure here: do you consider type changes that suddenly lead to fail in a code that passed before not a breaking change? Do you only consider runtime fail a breaking change? Because the record change was a breakage on type level. Whoever had previously The idea behind the option Now you take away the intention of |
I believe what you're describing (the change to make record values
I absolutely consider both cases breaking changes, and we've accidentally broken code in the past, which is something I'd like to be more careful about in the future. There are exceptions to this: sometimes TypeScript versions can break our assumptions about how a feature works, and it can be hard to head off those changes in a flexible way. We'll do our best to react in those circumstances. |
Its the same problem here, but with refinements. You're correct. Same points explained apply on those too. |
How about a new function (instead of reverting/fixing/introducing breaking changes)? Maybe |
Moving my complaint in #1540 here, because it sounds like the same issue. My specific problem is that I'm trying to parse values into this type: type FormattedText = { text: string; bold?: true; }; The // Valid values include
{ text: "foo" }
{ text: "foo", bold: true }
// Invalid values include
{ text: "foo", bold: false }
{ text: "foo", bold: undefined } This schema is designed to work with Slate, and the values are in a database. The design is therefore non-negotiable. I can't figure out how to configure zod to make the I realize I could do this with |
@igalklebanov's proposal goes the right direction. Not sure about the name though. It would have been perfect type-wise, if This might also similarly apply to |
I like the |
I think
If you really need to allow a property with an explicit const personSchema = z.object({
name: z.string().or(z.undefined())
});
type Person = z.infer<typeof personSchema>; Oops, the |
@zetaraku I think the problem is that while everything is fine type-wise when doing so, the code that runs at runtime need to know whether an explizit |
Since TypeScript actually differentiate I would prefer zod to act as if Usage like Reserving |
A separate entry point like this might be a good non-breaking solution for both settings. 🤔 import { z } from 'zod/strict'; |
@akomm Since the only difference is the runtime behavior of |
My main confusion here is why this is being treated as an "enhancement" and not a bug. Or worse, a regression: @akomm has indicated that this worked correctly in a previous version of zod. |
It seems that zod is designed around an assumed TypeScript config that its users are supposed to set. This makes sense: you can't design for all But is it documented somewhere what TypeScript config zod is designed for, and why? As a design principle, I would design zod around the "strictest" form of TypeScript, i.e. for users that are using TypeScript effectively. So is there a particular reason that zod is designed for users with |
@jameshfisher 99% agree. It I see where certain issues are with the feature, but there are solutions as discussed here. Probably this BC breakage would not have happened if author had not closed the topics that were discussing it, just to later add I especially agree with the fact that adding namespaces feels like an overkill. However, because its not part of |
I want to add another example where this feature is useful but the change that was made to enforce |
@akomm are you able to point us to the prior closed discussion on this topic, and/or the PR that changed the design of This thread is missing any input from people who want the current design. Or are there not any advocates for the current design? (And therefore another reason that we should just fix the bug?) |
@jameshfisher there were a few, but one that has been reopened One thing I want to add though: Just because nobody comes up with a different expectation doesn't mean there are no. As long as the behavior is okay for them, they won't look for topics changing it. Until the breaking change. This should have triggered people, who did not explicitly check |
Hi!
Zod is adding the
undefined
type to all keys that are optional when using the different.partial()
modifiers.How about differentiating them by being present and undefined or just not present at all like typescript does with
exactOptionalPropertyTypes: true
.Would it be possible to implement in zod? I guess it could be the default since if you don't care you don't care. Also just using the typescript
Partial
type will actually honorexactOptionalPropertyTypes
by default (on the type level that is).The text was updated successfully, but these errors were encountered: