# zod

前言

Zod 是一个以 TypeScript 为首的模式声明和验证库

npm i zod -D
1

# 1.使用

const userSchema = z.object({
  name: z.string(),
  age: z.number().optional(), // 年龄是可选的,可写可不写
});
1
2
3
4
const userSchema = z.object({
  name: z.string(),
  age: z.number(),
});

type User = z.infer<typeof userSchema>;

const user: User = { name: "Tom", age: 25 };
1
2
3
4
5
6
7
8