Skip to content

Commit

Permalink
fix: return type.
Browse files Browse the repository at this point in the history
  • Loading branch information
Akira Higuchi committed Aug 5, 2022
1 parent 1981d5e commit 0b4bd54
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const schema = z.object({
age: z.number().min(10),
});
// create default values with zod-empty
const defaultValues = init<z.infer<typeof schema>>(schema);
const defaultValues = init(schema);

const App = () => {
const {
Expand Down
2 changes: 1 addition & 1 deletion samples/react-hook-form/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const schema = z.object({
age: z.number().min(10).default(100),
});
// create default values with zod-empty
const defaultValues = init<z.infer<typeof schema>>(schema);
const defaultValues = init(schema);

const App = () => {
const {
Expand Down
2 changes: 1 addition & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe("make empty", () => {
.args(z.number(), z.string())
.returns(z.boolean());
type SchemaType = z.infer<typeof schema>;
expect(init<SchemaType>(schema)(0, "")).toBe(false);
expect(init(schema)(0, "")).toBe(false);
});

it("tuple", () => {
Expand Down
21 changes: 9 additions & 12 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/* eslint @typescript-eslint/no-explicit-any: off */
import clone from "just-clone";
import type { ZodTypeAny } from "zod";
import type { input, ZodTypeAny } from "zod";

function make<T extends ZodTypeAny>(schema: T): unknown {
function init<T extends ZodTypeAny>(schema: T): input<T> {
const def = schema._def;

switch (def.typeName) {
case "ZodObject":
const outputObject: Record<string, any> = {};
Object.entries(def.shape()).forEach(
([key, value]) => (outputObject[key] = make(value as ZodTypeAny))
([key, value]) => (outputObject[key] = init(value as ZodTypeAny))
);
return outputObject;
case "ZodRecord":
Expand All @@ -32,11 +32,11 @@ function make<T extends ZodTypeAny>(schema: T): unknown {
case "ZodLiteral":
return def.value;
case "ZodEffects":
return make(def.schema);
return init(def.schema);
case "ZodArray":
return [];
case "ZodTuple":
return def.items.map((item: ZodTypeAny) => make(item));
return def.items.map((item: ZodTypeAny) => init(item));
case "ZodSet":
return new Set();
case "ZodMap":
Expand All @@ -49,14 +49,14 @@ function make<T extends ZodTypeAny>(schema: T): unknown {
(value) => typeof def.values[value as any] !== "number"
)[0];
case "ZodUnion":
return make(def.options[0]);
return init(def.options[0]);
case "ZodDiscriminatedUnion":
return make(Array.from(def.options.values() as any[])[0]);
return init(Array.from(def.options.values() as any[])[0]);
case "ZodIntersection":
return Object.assign(make(def.left) as any, make(def.right));
return Object.assign(init(def.left) as any, init(def.right));
case "ZodFunction":
// eslint-disable-next-line @typescript-eslint/no-unused-vars
return (..._: any[]) => make(def.returns);
return (..._: any[]) => init(def.returns);
case "ZodDefault":
return def.innerType._def.typeName === "ZodFunction"
? def.defaultValue()
Expand All @@ -76,7 +76,4 @@ function make<T extends ZodTypeAny>(schema: T): unknown {
return undefined;
}
}
function init<R = unknown>(schema: any): R {
return make(schema) as R;
}
export default init;

0 comments on commit 0b4bd54

Please sign in to comment.