Skip to content

Commit 61e9748

Browse files
authored
add never props to safe parse return types (#3295)
1 parent 167047b commit 61e9748

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

deno/lib/__tests__/safeparse.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ const stringSchema = z.string();
88
test("safeparse fail", () => {
99
const safe = stringSchema.safeParse(12);
1010
expect(safe.success).toEqual(false);
11-
expect((safe as any).error).toBeInstanceOf(z.ZodError);
11+
expect(safe.error).toBeInstanceOf(z.ZodError);
1212
});
1313

1414
test("safeparse pass", () => {
1515
const safe = stringSchema.safeParse("12");
1616
expect(safe.success).toEqual(true);
17-
expect((safe as any).data).toEqual("12");
17+
expect(safe.data).toEqual("12");
1818
});
1919

2020
test("safeparse unexpected error", () => {

deno/lib/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,16 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
144144
return { errorMap: customMap, description };
145145
}
146146

147-
export type SafeParseSuccess<Output> = { success: true; data: Output };
148-
export type SafeParseError<Input> = { success: false; error: ZodError<Input> };
147+
export type SafeParseSuccess<Output> = {
148+
success: true;
149+
data: Output;
150+
error?: never;
151+
};
152+
export type SafeParseError<Input> = {
153+
success: false;
154+
error: ZodError<Input>;
155+
data?: never;
156+
};
149157

150158
export type SafeParseReturnType<Input, Output> =
151159
| SafeParseSuccess<Output>

src/__tests__/safeparse.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ const stringSchema = z.string();
77
test("safeparse fail", () => {
88
const safe = stringSchema.safeParse(12);
99
expect(safe.success).toEqual(false);
10-
expect((safe as any).error).toBeInstanceOf(z.ZodError);
10+
expect(safe.error).toBeInstanceOf(z.ZodError);
1111
});
1212

1313
test("safeparse pass", () => {
1414
const safe = stringSchema.safeParse("12");
1515
expect(safe.success).toEqual(true);
16-
expect((safe as any).data).toEqual("12");
16+
expect(safe.data).toEqual("12");
1717
});
1818

1919
test("safeparse unexpected error", () => {

src/types.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,16 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
144144
return { errorMap: customMap, description };
145145
}
146146

147-
export type SafeParseSuccess<Output> = { success: true; data: Output };
148-
export type SafeParseError<Input> = { success: false; error: ZodError<Input> };
147+
export type SafeParseSuccess<Output> = {
148+
success: true;
149+
data: Output;
150+
error?: never;
151+
};
152+
export type SafeParseError<Input> = {
153+
success: false;
154+
error: ZodError<Input>;
155+
data?: never;
156+
};
149157

150158
export type SafeParseReturnType<Input, Output> =
151159
| SafeParseSuccess<Output>

0 commit comments

Comments
 (0)