Building Zod from zero to improve my expertise in TypeScript
- String (w/ only Optional & Nullable)
- Boolean (w/ only Optional & Nullable)
- Number (w/ only Optional & Nullable)
- Object (w/ Optional & Nullable)
- Nullable
- Optional
- Infer (String, Boolean, Number, Object, Optional, Nullable, Arrray)
- Array
- Parse
- Min / Max / Length
- Combine Optional / Nullable / Array
/**
* Définition du schéma de l'utilisateur
*/
const demoUserSchema = TSV.construct({
ids: TSV.array(TSV.string())
name: TSV.string(),
age: TSV.nullable(TSV.number()),
isSubscribed: TSV.optional(TSV.boolean()),
address: TSV.construct({
street: TSV.string(),
city: TSV.string(),
country: TSV.string(),
}).optional(),
});
/**
* Récupération du type de l'utilisateur depuis le schéma
*/
type DemoUser = TSV.Infer<typeof demoUserSchema>;
/**
* Format du type infer depuis le schéma
*
* type DemoUser = {
* ids: string[]
* name: string;
* age: number | null;
* isSubscribed?: boolean | undefined;
* address?: {
* street: string;
* city: string;
* country: string;
* } | undefined;
* }
*
*/