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
65
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
54
Prisma the ORM that node was waiting for
puppo
0
78
How to scale up your projects like a pro
puppo
0
110
Alive, Tipi Sopravvisuti
puppo
0
79
ReactDay 2022 - Unit testing
puppo
0
88
How to scale up your angular projects like a pro
puppo
0
92
Prisma in the Air
puppo
0
290
Can typescript definitions survive even at runtime?
puppo
0
96
Is it possible to build your UI components using only web components?
puppo
0
68
Other Decks in Programming
See All in Programming
障害対応を起点としたもっといい開発と運用のサイクル作りのためにできること / Hatena Enginner Seminar #29
polamjag
0
300
Random\Randomizer クラスで日常のあれこれを解決しよう! / Random\Randomizer class solves familiar trouble
cocoeyes02
0
260
Scalable Customer Journey Orchestration (CJO)
lewuathe
0
380
Node.js v22 で変わること
yosuke_furukawa
PRO
10
3.7k
Tailwind CSSを本気でカスタマイズする方法
fsubal
14
5.4k
初心者のためのRubyKaigi入門/RubyKaigi Introduction
a_matsuda
7
1.2k
サイコロで理解する統計的仮説検定の考え方
tatamiya
4
1k
Next.js App Router
quramy
7
700
Netty Chicago Java User Group 2024-04-17
sullis
0
190
Build Apps for iOS, Android & Desktop in 100% Kotlin With Compose Multiplatform (mDevCamp 2024)
zsmb
0
400
VS Code をプロダクトにどう取り込むか
onomax
1
530
SwiftUIで使いやすいToastの作り方 / How to build a Toast system which is easy to use in SwiftUI
lovee
3
150
Featured
See All Featured
RailsConf 2023
tenderlove
7
550
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
123
39k
The World Runs on Bad Software
bkeepers
PRO
61
6.7k
Clear Off the Table
cherdarchuk
85
310k
The Mythical Team-Month
searls
216
42k
Adopting Sorbet at Scale
ufuk
69
8.6k
Building an army of robots
kneath
300
41k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
14
1.5k
Visualization
eitanlees
137
14k
Designing for humans not robots
tammielis
248
25k
jQuery: Nuts, Bolts and Bling
dougneiner
59
7.1k
Sharpening the Axe: The Primacy of Toolmaking
bcantrill
20
1.4k
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]