Skip to content

Commit 773b212

Browse files
committed
FIX: no longer adding undefined for ZodOptional.
1 parent c4ab005 commit 773b212

5 files changed

Lines changed: 25 additions & 28 deletions

File tree

example/example.client.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
type Type1 = {
22
title: string;
3-
features?: Type1[] | undefined;
3+
features?: Type1[];
44
};
55

66
type SomeOf<T> = T[keyof T];
@@ -265,15 +265,15 @@ interface PostV1AvatarRawNegativeResponseVariants {
265265
/** get /v1/events/stream */
266266
type GetV1EventsStreamInput = {
267267
/** @deprecated for testing error response */
268-
trigger?: string | undefined;
268+
trigger?: string;
269269
};
270270

271271
/** get /v1/events/stream */
272272
type GetV1EventsStreamPositiveVariant1 = {
273273
data: number;
274274
event: "time";
275-
id?: string | undefined;
276-
retry?: number | undefined;
275+
id?: string;
276+
retry?: number;
277277
};
278278

279279
/** get /v1/events/stream */

express-zod-api/src/zts.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ const makeSample = (produced: ts.TypeNode) =>
119119
samples?.[produced.kind as keyof typeof samples];
120120

121121
const onOptional: Producer = ({ _zod: { def } }: $ZodOptional, { next }) =>
122-
f.createUnionTypeNode([
123-
next(def.innerType),
124-
ensureTypeNode(ts.SyntaxKind.UndefinedKeyword),
125-
]);
122+
next(def.innerType);
126123

127124
const onNullable: Producer = ({ _zod: { def } }: $ZodNullable, { next }) =>
128125
f.createUnionTypeNode([next(def.innerType), makeLiteralType(null)]);

express-zod-api/tests/__snapshots__/integration.spec.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,14 +282,14 @@ exports[`Integration > Should treat optionals the same way as z.infer() by defau
282282
283283
/** post /v1/test-with-dashes */
284284
type PostV1TestWithDashesInput = {
285-
opt?: string | undefined;
285+
opt?: string;
286286
};
287287
288288
/** post /v1/test-with-dashes */
289289
type PostV1TestWithDashesPositiveVariant1 = {
290290
status: "success";
291291
data: {
292-
similar?: number | undefined;
292+
similar?: number;
293293
};
294294
};
295295

express-zod-api/tests/__snapshots__/zts.spec.ts.snap

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ exports[`zod-to-ts > Example > should produce the expected results 1`] = `
2222
any: any;
2323
unknown: any;
2424
never: any;
25-
optionalString?: string | undefined;
25+
optionalString?: string;
2626
nullablePartialObject: {
27-
string?: string | undefined;
28-
number?: number | undefined;
29-
fixedArrayOfString?: string[] | undefined;
27+
string?: string;
28+
number?: number;
29+
fixedArrayOfString?: string[];
3030
object?: {
3131
string: string;
32-
} | undefined;
32+
};
3333
} | null;
3434
tuple: [
3535
string,
@@ -57,8 +57,8 @@ exports[`zod-to-ts > Example > should produce the expected results 1`] = `
5757
set: any;
5858
intersection: (string & number) | bigint;
5959
promise: any;
60-
optDefaultString?: string | undefined;
61-
refinedStringWithSomeBullshit: (string | number) & ((bigint | null) | undefined);
60+
optDefaultString?: string;
61+
refinedStringWithSomeBullshit: (string | number) & (bigint | null);
6262
nativeEnum: "A" | "apple" | "banana" | "cantaloupe" | 5;
6363
lazy: SomeType;
6464
discUnion: {
@@ -127,15 +127,15 @@ exports[`zod-to-ts > Issue #2352: intersection of objects having same prop %# >
127127
"{
128128
query: string;
129129
} & {
130-
query?: string | undefined;
130+
query?: string;
131131
}"
132132
`;
133133

134134
exports[`zod-to-ts > Issue #2352: intersection of objects having same prop %# > should not flatten the result for objects with a conflicting prop 3 1`] = `
135135
"{
136136
query: string;
137137
} & {
138-
query?: string | undefined;
138+
query?: string;
139139
}"
140140
`;
141141

@@ -268,21 +268,21 @@ exports[`zod-to-ts > z.object() > supports zod.describe() 1`] = `
268268
}"
269269
`;
270270

271+
exports[`zod-to-ts > z.optional() > Zod 4: does not add undefined to it, unwrap as is 1`] = `"string"`;
272+
271273
exports[`zod-to-ts > z.optional() > Zod 4: should add question mark only to optional props 1`] = `
272274
"{
273-
optional?: string | undefined;
275+
optional?: string;
274276
required: string;
275-
transform: number | undefined;
276-
or: (number | undefined) | string;
277+
transform: number;
278+
or: number | string;
277279
tuple?: [
278-
string | undefined,
280+
string,
279281
number,
280282
{
281-
optional?: string | undefined;
283+
optional?: string;
282284
required: string;
283285
}
284-
] | undefined;
286+
];
285287
}"
286288
`;
287-
288-
exports[`zod-to-ts > z.optional() > outputs correct typescript 1`] = `"string | undefined"`;

express-zod-api/tests/zts.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ describe("zod-to-ts", () => {
202202
.optional(),
203203
});
204204

205-
test("outputs correct typescript", () => {
205+
test("Zod 4: does not add undefined to it, unwrap as is", () => {
206206
const node = zodToTs(optionalStringSchema, { ctx });
207207
expect(printNodeTest(node)).toMatchSnapshot();
208208
});

0 commit comments

Comments
 (0)