z.union.default Has Unexpected Type Error
See original GitHub issueFirst of all, thanks for this excellent library. I’m currently tying too hard to integrate it everywhere I possibly can haha
I found a rather minor type issue. Not sure if I’m misusing it, but it seems to work as expected at run-time — just not at compile-time. When using z.union
, using .optional()
seems to not be possible, at least in this case that I encountered and simplified into an example. As far as I can tell, runtime seems to work as-expected after adding a @ts-expect-error
.
Reproduce
Link: TS Playground
import { z } from "zod";
enum Kind {
ANIMAL = "ANIMAL",
FLOWER = "FLOWER",
}
const x = z
.union([
z.object({ kind: z.literal("animal"), legs: z.number() }),
z.object({ kind: z.literal("flower"), flowers: z.number() }),
])
.default(() => ({ kind: "animal", legs: 4 }));
const y = z
.union([
z.object({ kind: z.literal(Kind.ANIMAL), legs: z.number() }),
z.object({ kind: z.literal(Kind.FLOWER), flowers: z.number() }),
])
.default(() => ({ kind: Kind.ANIMAL, legs: 4 }));
Expected Behavior
No type error is produced
Actual Behavior
x
No overload matches this call.
Overload 1 of 2, '(def: { kind: "animal"; legs: number; } | { kind: "flower"; flowers: number; }): ZodDefault<ZodUnion<[ZodObject<{ kind: ZodLiteral<"animal">; legs: ZodNumber; }, "strip", ZodTypeAny, { ...; }, { ...; }>, ZodObject<...>]>>', gave the following error.
Argument of type '() => { kind: string; legs: number; }' is not assignable to parameter of type '{ kind: "animal"; legs: number; } | { kind: "flower"; flowers: number; }'.
Type '() => { kind: string; legs: number; }' is missing the following properties from type '{ kind: "flower"; flowers: number; }': kind, flowers
Overload 2 of 2, '(def: () => { kind: "animal"; legs: number; } | { kind: "flower"; flowers: number; }): ZodDefault<ZodUnion<[ZodObject<{ kind: ZodLiteral<"animal">; legs: ZodNumber; }, "strip", ZodTypeAny, { ...; }, { ...; }>, ZodObject<...>]>>', gave the following error.
Type 'string' is not assignable to type '"animal" | "flower"'.(2769)
y
No overload matches this call.
Overload 1 of 2, '(def: { kind: Kind.ANIMAL; legs: number; } | { kind: Kind.FLOWER; flowers: number; }): ZodDefault<ZodUnion<[ZodObject<{ kind: ZodLiteral<Kind.ANIMAL>; legs: ZodNumber; }, "strip", ZodTypeAny, { ...; }, { ...; }>, ZodObject<...>]>>', gave the following error.
Argument of type '() => { kind: Kind; legs: number; }' is not assignable to parameter of type '{ kind: Kind.ANIMAL; legs: number; } | { kind: Kind.FLOWER; flowers: number; }'.
Type '() => { kind: Kind; legs: number; }' is missing the following properties from type '{ kind: Kind.FLOWER; flowers: number; }': kind, flowers
Overload 2 of 2, '(def: () => { kind: Kind.ANIMAL; legs: number; } | { kind: Kind.FLOWER; flowers: number; }): ZodDefault<ZodUnion<[ZodObject<{ kind: ZodLiteral<Kind.ANIMAL>; legs: ZodNumber; }, "strip", ZodTypeAny, { ...; }, { ...; }>, ZodObject<...>]>>', gave the following error.
Type '{ kind: Kind; legs: number; }' is not assignable to type '{ kind: Kind.ANIMAL; legs: number; } | { kind: Kind.FLOWER; flowers: number; }'.
Type '{ kind: Kind; legs: number; }' is not assignable to type '{ kind: Kind.ANIMAL; legs: number; }'.
Types of property 'kind' are incompatible.
Type 'Kind' is not assignable to type 'Kind.ANIMAL'.(2769)
Versions
TypeScript: 4.4.2
zod: 3.8.2
Issue Analytics
- State:
- Created 2 years ago
- Comments:7 (3 by maintainers)
Top Results From Across the Web
Pylance: unexpected type inference for a union of two list types
Use a constrained generic type variable: (PEP 484) from typing import TypeVar T = TypeVar("T", str, int) def my_set(x: list[T]) -> set[T]: ...
Read more >Unexpected missing key errors with intersections/unions that ...
I'm getting .parse errors that seem incorrect with a schema similar to this one: ... import * as z from "zod"; type UnionType<T...
Read more >Troubleshoot Analytics Extension Connections - Tableau Help
Errors that can occur when communicating with any analytics extension. Error Message, Comments. Unsupported type passed as an argument to SCRIPT function.
Read more >Anonymous Struct Literals - Zig Programming Language
Error Unions. If a is an error , returns b ("default value"), otherwise returns the unwrapped value of a . Note that b...
Read more >Warning Options (Using the GNU Compiler Collection (GCC))
union U is now always passed in memory. -Wchar-subscripts. Warn if an array subscript has type char . This is a common cause...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
I think this issue here is resolved, during which another new issue about docs emerged. Maybe it’s better to close this one and create a new issue?