diff --git a/deno/lib/__tests__/discriminated-unions.test.ts b/deno/lib/__tests__/discriminated-unions.test.ts index bff0cdac2..bf090db1f 100644 --- a/deno/lib/__tests__/discriminated-unions.test.ts +++ b/deno/lib/__tests__/discriminated-unions.test.ts @@ -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 }); +}); diff --git a/deno/lib/types.ts b/deno/lib/types.ts index 19bbdd6c9..1fed75de2 100644 --- a/deno/lib/types.ts +++ b/deno/lib/types.ts @@ -3165,7 +3165,7 @@ export type ZodDiscriminatedUnionOption = export interface ZodDiscriminatedUnionDef< Discriminator extends string, - Options extends ZodDiscriminatedUnionOption[] = ZodDiscriminatedUnionOption[] + Options extends readonly ZodDiscriminatedUnionOption[] = ZodDiscriminatedUnionOption[] > extends ZodTypeDef { discriminator: Discriminator; options: Options; @@ -3175,7 +3175,7 @@ export interface ZodDiscriminatedUnionDef< export class ZodDiscriminatedUnion< Discriminator extends string, - Options extends ZodDiscriminatedUnionOption[] + Options extends readonly ZodDiscriminatedUnionOption[] > extends ZodType< output, ZodDiscriminatedUnionDef, @@ -3245,7 +3245,7 @@ export class ZodDiscriminatedUnion< */ static create< Discriminator extends string, - Types extends [ + Types extends readonly [ ZodDiscriminatedUnionOption, ...ZodDiscriminatedUnionOption[] ] diff --git a/src/__tests__/discriminated-unions.test.ts b/src/__tests__/discriminated-unions.test.ts index 9d8e7b6ce..58698e7be 100644 --- a/src/__tests__/discriminated-unions.test.ts +++ b/src/__tests__/discriminated-unions.test.ts @@ -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 }); +}); diff --git a/src/types.ts b/src/types.ts index bbb857a47..24e12299e 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3165,7 +3165,7 @@ export type ZodDiscriminatedUnionOption = export interface ZodDiscriminatedUnionDef< Discriminator extends string, - Options extends ZodDiscriminatedUnionOption[] = ZodDiscriminatedUnionOption[] + Options extends readonly ZodDiscriminatedUnionOption[] = ZodDiscriminatedUnionOption[] > extends ZodTypeDef { discriminator: Discriminator; options: Options; @@ -3175,7 +3175,7 @@ export interface ZodDiscriminatedUnionDef< export class ZodDiscriminatedUnion< Discriminator extends string, - Options extends ZodDiscriminatedUnionOption[] + Options extends readonly ZodDiscriminatedUnionOption[] > extends ZodType< output, ZodDiscriminatedUnionDef, @@ -3245,7 +3245,7 @@ export class ZodDiscriminatedUnion< */ static create< Discriminator extends string, - Types extends [ + Types extends readonly [ ZodDiscriminatedUnionOption, ...ZodDiscriminatedUnionOption[] ]