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
81
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
87
How to scale up your projects like a pro
puppo
0
110
Alive, Tipi Sopravvisuti
puppo
0
88
ReactDay 2022 - Unit testing
puppo
0
99
How to scale up your angular projects like a pro
puppo
0
120
Prisma in the Air
puppo
0
370
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
Kaigi on Rails 2024 〜運営の裏側〜
krpk1900
1
230
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Better Code Design in PHP
afilina
PRO
0
130
Laravel や Symfony で手っ取り早く OpenAPI のドキュメントを作成する
azuki
2
120
Creating a Free Video Ad Network on the Edge
mizoguchicoji
0
120
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.2k
レガシーシステムにどう立ち向かうか 複雑さと理想と現実/vs-legacy
suzukihoge
14
2.2k
エンジニアとして関わる要件と仕様(公開用)
murabayashi
0
300
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
聞き手から登壇者へ: RubyKaigi2024 LTでの初挑戦が 教えてくれた、可能性の星
mikik0
1
130
Amazon Qを使ってIaCを触ろう!
maruto
0
410
Featured
See All Featured
Building a Scalable Design System with Sketch
lauravandoore
459
33k
The Cost Of JavaScript in 2023
addyosmani
45
6.8k
Designing the Hi-DPI Web
ddemaree
280
34k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
38
1.8k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
42
9.2k
Product Roadmaps are Hard
iamctodd
PRO
49
11k
Typedesign – Prime Four
hannesfritz
40
2.4k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Embracing the Ebb and Flow
colly
84
4.5k
What's new in Ruby 2.0
geeforr
343
31k
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]