Skip to content

Commit 70e48cf

Browse files
committed
refactor: api for $$Function
1 parent e42ffb1 commit 70e48cf

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/__tests__/zod.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ describe('$AnyFunction', () => {
239239
});
240240

241241
describe('$$Function', () => {
242-
const $Schema = $$Function({ input: z4.tuple([z4.number(), z4.number()]), output: z4.number() });
242+
const $Schema = $$Function({ input: [z4.number(), z4.number()], output: z4.number() });
243243
it('should be correctly typed', () => {
244244
expectTypeOf<z4.infer<typeof $Schema>>().toEqualTypeOf<(arg_0: number, arg_1: number) => number>();
245245
});

src/zod.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,18 +121,24 @@ export const $Uint8ArrayLike: z4.ZodType<Uint8Array> = z4
121121

122122
export const $AnyFunction = z4.custom<(...args: any[]) => any>((arg) => typeof arg === 'function', 'must be function');
123123

124-
export const $$Function = <TInput extends z4.ZodType<unknown[], unknown[]>, TOutput extends z4.ZodType>(params?: {
124+
export const $$Function = <TInput extends [z4.ZodType, ...z4.ZodType[]], TOutput extends z4.ZodType>({
125+
input,
126+
output
127+
}: {
125128
input: TInput;
126129
output: TOutput;
127-
}): z4.ZodType<(...args: z4.output<TInput>) => z4.output<TOutput>> => {
128-
const $Schema = z4.function(params);
130+
}): z4.ZodType<(...args: z4.output<z4.ZodTuple<TInput, null>>) => z4.output<TOutput>> => {
131+
const $Schema = z4.function({
132+
input: z4.tuple(input),
133+
output
134+
});
129135
return z4.custom().transform((arg, ctx) => {
130136
if (typeof arg !== 'function') {
131137
ctx.addIssue('Must be function');
132138
return z4.NEVER;
133139
}
134140
return $Schema.implement(arg as (...args: any[]) => any);
135-
}) as z4.ZodType<(...args: z4.output<TInput>) => z4.output<TOutput>>;
141+
}) as z4.ZodType<(...args: z4.output<z4.ZodTuple<TInput, null>>) => z4.output<TOutput>>;
136142
};
137143

138144
export function safeParse<TSchema extends z4.ZodTypeAny>(

0 commit comments

Comments
 (0)