Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
TypeScript で Railway Oriented Programming 型安全なエ...
Search
daitasu
March 19, 2025
550
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
TypeScript で Railway Oriented Programming 型安全なエラーハンドリングを作る
2025.3.19 コネヒトさんとのクローズドイベント
元記事
https://daitasu.hatenablog.jp/entry/2025/03/04/090000
daitasu
March 19, 2025
More Decks by daitasu
See All by daitasu
Semantic Version 単位で戦略を柔軟に変えて、パッケージアップデートを自動化する
daitasu
1
450
Tachikawa.any 運営挨拶
daitasu
0
330
「Tachikawa.any」始めました! 〜地域コミュニティをなぜ立ち上げるのか、初めての立ち上げの壁〜
daitasu
2
350
開発組織の課題解決を加速するための権限委譲 -する側、される側としての向き合い方-
daitasu
6
2.8k
ご機嫌に学べ - 寝ぼけ眼の親たちへ贈る_友の輪_駆動開発 -
daitasu
1
490
型安全で非依存な軽量AIエージェントフレームワーク、Tankstack AI
daitasu
1
170
AIエージェントが対話的なUIを返す!MCP−UIで変わるユーザ体験
daitasu
1
270
職種の壁を溶かして開発サイクルを高速に回す~情報透明性と職種越境から考えるAIフレンドリーな職種間連携~
daitasu
0
940
「みんな、笑顔になぁれ」を実現する 職種混合開発組織の目標設定・評価の改善事例
daitasu
0
950
Featured
See All Featured
The SEO Collaboration Effect
kristinabergwall1
1
550
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.6k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
2
3.8k
Pawsitive SEO: Lessons from My Dog (and Many Mistakes) on Thriving as a Consultant in the Age of AI
davidcarrasco
0
240
Navigating Weather and Climate Data
rabernat
0
510
How to Grow Your eCommerce with AI & Automation
katarinadahlin
PRO
1
270
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
690
Prompt Engineering for Job Search
mfonobong
0
450
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
Between Models and Reality
mayunak
4
450
Docker and Python
trallard
47
4.2k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
450
Transcript
TypeScript で Railway Oriented Programming 型安全なエラーハンドリングを作る コネヒト・千合同懇親会 @daitasu
TypeScript の例外処理に Railway Oritented Programming を使おう!
Railway Oriented Programming(ROP) • 2014年にScott Wlaschinさんによって提唱 • 関数型プログラミングにおける、エラーハンドリングの取り扱いに焦点を当てた 実装手法 •
関数を「成功」と「失敗」の2つの分岐がある 線路(レール) に見立てる
Railway Oriented Programming • 1つの関数が1つ のinput に対し、2つの output パターンを持つ 成功!
失敗... function input
Railway Oriented Programming • 「成功」時は次の処理へ、「失敗」時は何もせず通過する validateLength validateEmail on success bypass
Railway Oriented Programming • 各関数の連結 → 成功時のみ値が残り、失敗時は最初のエラー情報が最後 まで届く validateLength validateEmail
extractDomain
Result 型とは • ROP に則った型定義 • 言語によって命名が微妙に異なる • Result 型
◦ Success と Failure の Union 型 • Result 型を受けてResult型を返す処理も 一緒に必要(Scala だと flatMap 関数) ◦ Success 受取り時: 次の関数を実行 ◦ Failure 受取り時: 何もせず次へ
何が嬉しいの?
手続き的な例外処理のつらみ Error Handling を 各関数で意識する必要がある (考慮漏れの懸念) TSのtry catch は エラー情報の型を持たない
→ どのエラーか分からん
Result 型を適用する 返り値の型を Result 型へ → 型エラーで Handling 漏れに気付ける Success時:値が返却
Error時:エラー箇所の情報を保持 エラー処理の入れ子が減って、 処理が追いやすくなる
まとめ • 手続き的エラーハンドリングはしんどい ◦ try catch の入れ子の増加と対応漏れの発生 ◦ 例外の型が伝播されない •
Result はオススメ ◦ ROP によって例外処理の漏れを型安全に防止できる ◦ try catch の入れ子を脱せて、煩雑化を防げる • TS 標準の型ではない ◦ TS 標準の型ではなく、オレオレ型定義が生まれる ◦ 用法・用量を守って症状を見ながらの使用に留める
記事 https://daitasu.hatenablog.jp/entry/2025/03/04/090000 TypeScript の例外処理を Railway Oriented Programming(Result 型) で楽にしたい