Skip to content

Commit

Permalink
Allow creation of discriminated unions with a readonly array of options
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Haines <[email protected]>
  • Loading branch information
haines committed May 29, 2024
1 parent 62b7842 commit bf69980
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
11 changes: 11 additions & 0 deletions deno/lib/__tests__/discriminated-unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,3 +308,14 @@ test("optional and nullable", () => {
if (value.key === "b") value.b;
if (value.key === null) value.b;
});

test("readonly array of options", () => {
const options = [
z.object({ type: z.literal("x"), val: z.literal(1) }),
z.object({ type: z.literal("y"), val: z.literal(2) }),
] as const;

expect(
z.discriminatedUnion("type", options).parse({ type: "x", val: 1 })
).toEqual({ type: "x", val: 1 });
});
6 changes: 3 additions & 3 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3099,7 +3099,7 @@ export type ZodDiscriminatedUnionOption<Discriminator extends string> =

export interface ZodDiscriminatedUnionDef<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
Options extends readonly ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
> extends ZodTypeDef {
discriminator: Discriminator;
options: Options;
Expand All @@ -3109,7 +3109,7 @@ export interface ZodDiscriminatedUnionDef<

export class ZodDiscriminatedUnion<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<Discriminator>[]
Options extends readonly ZodDiscriminatedUnionOption<Discriminator>[]
> extends ZodType<
output<Options[number]>,
ZodDiscriminatedUnionDef<Discriminator, Options>,
Expand Down Expand Up @@ -3179,7 +3179,7 @@ export class ZodDiscriminatedUnion<
*/
static create<
Discriminator extends string,
Types extends [
Types extends readonly [
ZodDiscriminatedUnionOption<Discriminator>,
...ZodDiscriminatedUnionOption<Discriminator>[]
]
Expand Down
11 changes: 11 additions & 0 deletions src/__tests__/discriminated-unions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,14 @@ test("optional and nullable", () => {
if (value.key === "b") value.b;
if (value.key === null) value.b;
});

test("readonly array of options", () => {
const options = [
z.object({ type: z.literal("x"), val: z.literal(1) }),
z.object({ type: z.literal("y"), val: z.literal(2) }),
] as const;

expect(
z.discriminatedUnion("type", options).parse({ type: "x", val: 1 })
).toEqual({ type: "x", val: 1 });
});
6 changes: 3 additions & 3 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3099,7 +3099,7 @@ export type ZodDiscriminatedUnionOption<Discriminator extends string> =

export interface ZodDiscriminatedUnionDef<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
Options extends readonly ZodDiscriminatedUnionOption<string>[] = ZodDiscriminatedUnionOption<string>[]
> extends ZodTypeDef {
discriminator: Discriminator;
options: Options;
Expand All @@ -3109,7 +3109,7 @@ export interface ZodDiscriminatedUnionDef<

export class ZodDiscriminatedUnion<
Discriminator extends string,
Options extends ZodDiscriminatedUnionOption<Discriminator>[]
Options extends readonly ZodDiscriminatedUnionOption<Discriminator>[]
> extends ZodType<
output<Options[number]>,
ZodDiscriminatedUnionDef<Discriminator, Options>,
Expand Down Expand Up @@ -3179,7 +3179,7 @@ export class ZodDiscriminatedUnion<
*/
static create<
Discriminator extends string,
Types extends [
Types extends readonly [
ZodDiscriminatedUnionOption<Discriminator>,
...ZodDiscriminatedUnionOption<Discriminator>[]
]
Expand Down

0 comments on commit bf69980

Please sign in to comment.