Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Keep alive your typescript definitions using Zod

Keep alive your typescript definitions using Zod

Luca Del Puppo

January 16, 2023
Tweet

More Decks by Luca Del Puppo

Other Decks in Programming

Transcript

  1. Who I am Luca Del Puppo 
 (aka Puppo) 


    Full-Stack Developer @ Flowing a Claranet Company 
 and Microsoft MVP @puppo92 
 https://www.linkedin.com/in/lucadelpuppo/ 
 [email protected]
  2. ➔ Typescript enables type check but on BUILT - TIME

    ➔ After the build all your types disappears ➔ After the build all your considerations about types dissolve Why?
  3. import { z } from ‘zod’; const CustomerSchema = z.object({

    id: z.number(), name: z.string(), email: z.string(), phone: z.string(), }); Schema
  4. ➔ Parse throws an Error ➔ ZodError is the right

    error to check ➔ ZodError contains all the issues that describe the errors Something wrong?
  5. Create a layer between your app and the outside V

    a l i d a t i o n HTTP socket.io
  6. ➔ Typescript is awesome, but it’s good only on built

    time ➔ Create a validation layer between your app and the outside • Prevent execution of code with wrong data • You can get notifications if something is changed ➔ Using Zod, you can guarantee your types, both built-time and run-time Conclusion