Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Keep alive your typescript definitions using Zod
Search
Luca Del Puppo
January 16, 2023
Programming
0
110
Keep alive your typescript definitions using Zod
Luca Del Puppo
January 16, 2023
Tweet
Share
More Decks by Luca Del Puppo
See All by Luca Del Puppo
Aware dependencies using Nx Graph
puppo
0
72
Prisma the ORM that node was waiting for
puppo
0
91
How to scale up your projects like a pro
puppo
0
120
Alive, Tipi Sopravvisuti
puppo
0
97
ReactDay 2022 - Unit testing
puppo
0
100
How to scale up your angular projects like a pro
puppo
0
140
Prisma in the Air
puppo
0
410
Can typescript definitions survive even at runtime?
puppo
0
110
Is it possible to build your UI components using only web components?
puppo
0
81
Other Decks in Programming
See All in Programming
Agent Rules as Domain Parser
yodakeisuke
1
350
Efficiency and Rock 'n’ Roll (Really!)
hollycummins
0
600
複数アプリケーションを育てていくための共通化戦略
irof
2
720
DevTalks 25 - Create your own AI-infused Java apps with ease
kdubois
2
120
『Python → TypeScript』オンボーディング奮闘記
takumi_tatsuno
1
140
技術懸念に立ち向かい 法改正を穏便に乗り切った話
pop_cashew
0
830
がんばりすぎないコーディングルール運用術
tsukakei
1
180
TypeScript エンジニアが Android 開発の世界に飛び込んだ話
yuisakamoto
6
960
Feature Flag 自動お掃除のための TypeScript プログラム変換
azrsh
PRO
4
630
CQRS/ESのクラスとシステムフロー ~ RailsでフルスクラッチでCQRSESを組んで みたことから得た学び~
suzukimar
0
190
事業戦略を理解してソフトウェアを設計する
masuda220
PRO
7
1.3k
Cloudflare Realtime と Workers でつくるサーバーレス WebRTC
nekoya3
0
240
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
76
5.8k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
Why Our Code Smells
bkeepers
PRO
336
57k
Making Projects Easy
brettharned
116
6.2k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
1
80
Java REST API Framework Comparison - PWX 2021
mraible
31
8.6k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
42
2.3k
What's in a price? How to price your products and services
michaelherold
245
12k
Building a Scalable Design System with Sketch
lauravandoore
462
33k
We Have a Design System, Now What?
morganepeng
52
7.6k
Making the Leap to Tech Lead
cromwellryan
134
9.3k
Building Better People: How to give real-time feedback that sticks.
wjessup
368
19k
Transcript
Keep alive your typescript definitions using Zod Luca Del Puppo
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]
Typescript journey 1
10 years ago
The developers reaction
interface CustomerModel { id: number; name: string; email: string; phone:
string; } Interfaces
type CustomerModel = { id: number; name: string; email: string;
phone: string; }; Types
HTTP Request
But after a while…
➔ 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?
Prevent ugly mistakes 2
Zod is your super hero
import { z } from ‘zod’; const CustomerSchema = z.object({
id: z.number(), name: z.string(), email: z.string(), phone: z.string(), }); Schema
import { TypeOf } from ‘zod'; type CustomerModel = TypeOf<typeof
CustomerSchema>; Convert to type
const res = CustomerSchema.parse({ id: 1, name: 'John Doe', email:
'
[email protected]
', phone: '555-555-5555' }); Parse method
➔ Parse throws an Error ➔ ZodError is the right
error to check ➔ ZodError contains all the issues that describe the errors Something wrong?
Do you hate exceptions? use safeParse
safeParse Success ➔ success: true ➔ data: Output Error ➔
success: false ➔ error: ZodError
Benefits of using Zod
Create a layer between your app and the outside V
a l i d a t i o n HTTP socket.io
Demo 3
Conclusion 4
➔ 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
CODE SLIDE
Thanks! That’s All! Luca Del Puppo @puppo92 https://www.linkedin.com/in/lucadelpuppo/
[email protected]