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
78
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
63
Prisma the ORM that node was waiting for
puppo
0
86
How to scale up your projects like a pro
puppo
0
110
Alive, Tipi Sopravvisuti
puppo
0
87
ReactDay 2022 - Unit testing
puppo
0
97
How to scale up your angular projects like a pro
puppo
0
110
Prisma in the Air
puppo
0
350
Can typescript definitions survive even at runtime?
puppo
0
100
Is it possible to build your UI components using only web components?
puppo
0
73
Other Decks in Programming
See All in Programming
僕が思い描くTypeScriptの未来を勝手に先取りする
yukukotani
9
2.4k
LangGraphでのHuman-in-the-Loopの実装
os1ma
3
1.1k
マイグレーションコード自作して File-Based Routing に自動移行!! ~250 ページの歴史的経緯を添えて~
cut0
1
260
GoのIteratorに詳しくなってしまう
inatonix
1
200
Prompt Cachingは本当に効果的なのか検証してみた.pdf
ttnyt8701
0
530
React + TextAliveでカッコいいLyric Applicatioinを作ろう!!
tosuri13
0
400
Perl 5 OOP機構30年史 - Perl 5's OOP Mechanism over the past 30 years
moznion
0
250
AndroidアプリのUIバリエーションをあの手この手で確認する / Check UI variations of Android apps by various means
tkmnzm
1
170
Patched fetch did not work
quramy
4
380
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
0
120
Rubyのobject_id
qnighy
6
1.3k
o1モデルのプロンプトエンジニアリングって?
ktc_wada
0
260
Featured
See All Featured
Imperfection Machines: The Place of Print at Facebook
scottboms
263
13k
Clear Off the Table
cherdarchuk
91
320k
Web development in the modern age
philhawksworth
205
10k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.4k
The Brand Is Dead. Long Live the Brand.
mthomps
53
38k
Embracing the Ebb and Flow
colly
83
4.4k
YesSQL, Process and Tooling at Scale
rocio
167
14k
No one is an island. Learnings from fostering a developers community.
thoeni
18
2.9k
Designing on Purpose - Digital PM Summit 2013
jponch
114
6.8k
Testing 201, or: Great Expectations
jmmastey
36
7k
Why You Should Never Use an ORM
jnunemaker
PRO
53
8.9k
5 minutes of I Can Smell Your CMS
philhawksworth
202
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]