-
-
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
discriminatedUnion
produces TS error when .default
or .preprocess
are applied
#1490
Comments
This fixes type checking when passing literal types with default values or preprocess transformations. Before this change, the type checker was complaining that value does not exist, as described in colinhacks#1490. The fix consists in modifying the `ZodDiscriminatedUnionOption` to also allow `ZodDefault<ZodLiteral...>` and `ZodEffects<ZodLiteral...>`. Added a test case that had failing types but now works. Note: An even better solution I imagine is making sure that both `ZodDefault` and `ZodEffects` re-surface the inner type properties, such as `ZodLiteral`'s `value`. But I'd need more familiarity with the code base to propose that change.
Seems like there is a same problem if you refine one of the object schemas inside a union as well: z.discriminatedUnion('type', [
z.strictObject({
type: z.literal('foo'),
}),
z.strictObject({
type: z.literal('bar'),
}).refine((input) => true, 'Error'),
]); This one produces an error: Type 'ZodEffects<ZodObject<{ type: ZodLiteral<"bar">; }, "strict", ZodTypeAny, { type: "bar"; }, { type: "bar"; }>, { type: "bar"; }, { type: "bar"; }>' is missing the following properties from type 'ZodObject<{ type: ZodLiteral<Primitive>; } & ZodRawShape, any, any, { [x: string]: any; }, { [x: string]: any; }>': _cached, _getCached, shape, strict, and 14 more.
'input' is declared but its value is never read. And here's an ugly workaround for the refinement problem: const bar =
z.strictObject({
type: z.literal('bar'),
}).refine((input) => true, 'Error');
type ExtractRefinementType<T extends z.ZodTypeAny> = T extends z.ZodEffects<
infer T,
any,
any
>
? ExtractRefinementType<T>
: T;
z.discriminatedUnion('type', [
z.strictObject({
type: z.literal('foo'),
}),
bar as unknown as ExtractRefinementType<typeof bar>
]); |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
This would still be very nice to have! |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
@colinhacks I believe this may be marked as good first issue. |
Hey @RobertCraigie 👋
I was trying to default to a specific zod object if the |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
Not stale |
Still seeing this error when trying to apply a |
Still happening with refine too |
Playground link.
Given this snippet:
TypeScript produces this error:
This code works at runtime however and Zod correctly parses the object:
Previously opened as #1263 but was closed as stale.
The text was updated successfully, but these errors were encountered: