zod
Version:
Typescript-first schema declaration and validation library with static type inference
34 lines (27 loc) • 616 B
text/typescript
import * as z from '.';
// // interface Category {
// // name: string;
// // categories: Array<Category>;
// // }
// // const Cat: z.ZodType<Category> = z.lazy(() => {
// // // console.log(Cat);
// // return z.object({
// // name: z.string(),
// // categories: z.array(Cat),
// // });
// // });
const C = z.object({
foo: z.string(),
bar: z.number().optional(),
});
const f: any = { foo: 'asdf', bar: 1234 };
if (C.is(f)) {
console.log(`${f.bar}: ${f.foo}`);
} else {
console.log('No C');
}
// type C = z.TypeOf<typeof C>;
// /* {
// foo: string;
// bar?: number | undefined
// } */