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
94
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
66
Prisma the ORM that node was waiting for
puppo
0
87
How to scale up your projects like a pro
puppo
0
110
Alive, Tipi Sopravvisuti
puppo
0
91
ReactDay 2022 - Unit testing
puppo
0
100
How to scale up your angular projects like a pro
puppo
0
130
Prisma in the Air
puppo
0
390
Can typescript definitions survive even at runtime?
puppo
0
100
Is it possible to build your UI components using only web components?
puppo
0
77
Other Decks in Programming
See All in Programming
Go 1.24でジェネリックになった型エイリアスの紹介
syumai
2
300
複数のAWSアカウントから横断で 利用する Lambda Authorizer の作り方
tc3jp
0
120
はじめての Go * WASM *OCR
sgash708
1
110
CloudNativePGを布教したい
nnaka2992
0
120
『テスト書いた方が開発が早いじゃん』を解き明かす #phpcon_nagoya
o0h
PRO
9
2.5k
バッチを作らなきゃとなったときに考えること
irof
2
550
PEPCは何を変えようとしていたのか
ken7253
3
290
ファインディLT_ポケモン対戦の定量的分析
fufufukakaka
0
940
Lambdaの監視、できてますか?Datadogを用いてLambdaを見守ろう
nealle
2
520
Rubyと自由とAIと
yotii23
6
1.8k
機能が複雑化しても 頼りになる FactoryBotの話
tamikof
1
230
Ça bouge du côté des animations CSS !
goetter
2
160
Featured
See All Featured
Raft: Consensus for Rubyists
vanstee
137
6.8k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
134
33k
KATA
mclloyd
29
14k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
227
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.3k
Principles of Awesome APIs and How to Build Them.
keavy
126
17k
BBQ
matthewcrist
87
9.5k
We Have a Design System, Now What?
morganepeng
51
7.4k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
120k
Building Your Own Lightsaber
phodgson
104
6.2k
How to Ace a Technical Interview
jacobian
276
23k
Statistics for Hackers
jakevdp
797
220k
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/ luca@delpuppo.net
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:
'john@doe.com', 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/
luca@delpuppo.net