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
88
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
65
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
89
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
76
Other Decks in Programming
See All in Programming
DevinとCursorから学ぶAIエージェントメモリーの設計とMoatの考え方
itarutomy
0
150
カンファレンス動画鑑賞会のススメ / Osaka.swift #1
hironytic
0
170
DMMオンラインサロンアプリのSwift化
hayatan
0
190
Amazon Nova Reelの可能性
hideg
0
200
chibiccをCILに移植した結果 (NGK2025S版)
kekyo
PRO
0
140
“あなた” の開発を支援する AI エージェント Bedrock Engineer / introducing-bedrock-engineer
gawa
4
320
Simple組み合わせ村から大都会Railsにやってきた俺は / Coming to Rails from the Simple
moznion
3
2.2k
オニオンアーキテクチャを使って、 Unityと.NETでコードを共有する
soi013
0
370
ChatGPT とつくる PHP で OS 実装
memory1994
PRO
3
190
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
230
混沌とした例外処理とエラー監視に秩序をもたらす
morihirok
13
2.3k
快速入門可觀測性
blueswen
0
500
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
33
3k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
The Cult of Friendly URLs
andyhume
78
6.1k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.4k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
8
1.2k
Raft: Consensus for Rubyists
vanstee
137
6.7k
Six Lessons from altMBA
skipperchong
27
3.6k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
StorybookのUI Testing Handbookを読んだ
zakiyama
28
5.4k
How to Ace a Technical Interview
jacobian
276
23k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
59k
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]