Skip to content

Commit

Permalink
add never props to safe parse return types
Browse files Browse the repository at this point in the history
  • Loading branch information
schicks committed Mar 2, 2024
1 parent e5e8619 commit d6074be
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions deno/lib/__tests__/safeparse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ const stringSchema = z.string();
test("safeparse fail", () => {
const safe = stringSchema.safeParse(12);
expect(safe.success).toEqual(false);
expect((safe as any).error).toBeInstanceOf(z.ZodError);
expect(safe.error).toBeInstanceOf(z.ZodError);
});

test("safeparse pass", () => {
const safe = stringSchema.safeParse("12");
expect(safe.success).toEqual(true);
expect((safe as any).data).toEqual("12");
expect(safe.data).toEqual("12");
});

test("safeparse unexpected error", () => {
Expand Down
12 changes: 10 additions & 2 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,16 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
return { errorMap: customMap, description };
}

export type SafeParseSuccess<Output> = { success: true; data: Output };
export type SafeParseError<Input> = { success: false; error: ZodError<Input> };
export type SafeParseSuccess<Output> = {
success: true;
data: Output;
error?: never;
};
export type SafeParseError<Input> = {
success: false;
error: ZodError<Input>;
data?: never;
};

export type SafeParseReturnType<Input, Output> =
| SafeParseSuccess<Output>
Expand Down
4 changes: 2 additions & 2 deletions src/__tests__/safeparse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const stringSchema = z.string();
test("safeparse fail", () => {
const safe = stringSchema.safeParse(12);
expect(safe.success).toEqual(false);
expect((safe as any).error).toBeInstanceOf(z.ZodError);
expect(safe.error).toBeInstanceOf(z.ZodError);
});

test("safeparse pass", () => {
const safe = stringSchema.safeParse("12");
expect(safe.success).toEqual(true);
expect((safe as any).data).toEqual("12");
expect(safe.data).toEqual("12");
});

test("safeparse unexpected error", () => {
Expand Down
12 changes: 10 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,16 @@ function processCreateParams(params: RawCreateParams): ProcessedCreateParams {
return { errorMap: customMap, description };
}

export type SafeParseSuccess<Output> = { success: true; data: Output };
export type SafeParseError<Input> = { success: false; error: ZodError<Input> };
export type SafeParseSuccess<Output> = {
success: true;
data: Output;
error?: never;
};
export type SafeParseError<Input> = {
success: false;
error: ZodError<Input>;
data?: never;
};

export type SafeParseReturnType<Input, Output> =
| SafeParseSuccess<Output>
Expand Down

0 comments on commit d6074be

Please sign in to comment.