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
20250826 パターンマッチ
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
saka
December 24, 2025
20
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
20250826 パターンマッチ
saka
December 24, 2025
More Decks by saka
See All by saka
20250304 関数型DDD 6
sakatttttta
0
25
20241219 Github Actionsの面倒いところ
sakatttttta
0
18
20250109 関数型
sakatttttta
0
11
20250116 関数型DDD 2
sakatttttta
0
11
20250123 関数型DDD 3
sakatttttta
0
11
20250206 関数型DDD 4
sakatttttta
0
12
20250213 関数型DDD 4
sakatttttta
0
12
20250220 Hono カスタムfetch
sakatttttta
0
10
20250225 関数型DDD 5
sakatttttta
0
9
Featured
See All Featured
Paper Plane (Part 1)
katiecoart
PRO
2
12k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
123
22k
Making the Leap to Tech Lead
cromwellryan
135
10k
Producing Creativity
orderedlist
PRO
348
41k
Information Architects: The Missing Link in Design Systems
soysaucechin
1
1.2k
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4.2k
How Fast Is Fast Enough? [PerfNow 2025]
tammyeverts
3
940
Imperfection Machines: The Place of Print at Facebook
scottboms
270
14k
SERP Conf. Vienna - Web Accessibility: Optimizing for Inclusivity and SEO
sarafernandez
2
1.6k
Rebuilding a faster, lazier Slack
samanthasiow
85
9.7k
The agentic SEO stack - context over prompts
schlessera
0
970
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
21
1.7k
Transcript
ύλʔϯϚον
ύλʔϯϚονͱ
import { match } from "ts-pattern"; type Stationery = "Pen"
| "Pencil"; const useSound = (item: Stationery) => { return match(item) .with("Pen", () => "Χνο") // ϊοΫࣜϖϯͷΠϝʔδ .with("Pencil", () => "αϥαϥ") // ॻ͖৺ͷΠϝʔδ .exhaustive(); // શͯͷύλʔϯ͕ཏ͞Ε͍ͯΔ͔νΣοΫ͢Δ }; useSound("Pen"); // "Χνο" useSound("Eraser"); // Type Error: '"Eraser"' is not assignable to type 'Stationery'
͍͍ײ͡ͷػೳ
TSʹͳ͍
1. ϥΠϒϥϦ
ts-pattern
import { match } from "ts-pattern"; type Stationery = "Pen"
| "Pencil"; const useSound = (item: Stationery) => { return match(item) .with("Pen", () => "Χνο") // ϊοΫࣜϖϯͷΠϝʔδ .with("Pencil", () => "αϥαϥ") // ॻ͖৺ͷΠϝʔδ .exhaustive(); // શͯͷύλʔϯ͕ཏ͞Ε͍ͯΔ͔νΣοΫ͢Δ }; useSound("Pen"); // "Χνο" useSound("Eraser"); // Type Error: '"Eraser"' is not assignable to type 'Stationery'
2. ࣗલ
ϢχΦϯܕΛͬͯ
ϔϧύʔؔ
type Stationery = "Pen" | "Pencil"; const assertNever = (_:
never): never => { throw new Error(); }; const check = (item: Stationery) => { switch (item) { case "Pen": return "Χνο"; case "Pencil": return "αϥαϥ"; default: return assertNever(item); } };
Ͳ͏ͳΔ͔ʁ
None
Ҿ͕never͔͠ड͚औΕͳ͍ ʹ Կ͔ड͚औͬͨΒΤϥʔΛు͘
ΔͭΓͷ͍ํ
ΤϥʔΛϢχΦϯܕͰఆ ٛ
type DogError = { kind: "DogError"; message: string; }; type
CatError = { kind: "CatError"; message: string; }; type AnimalError = DogError | CatError;
ResultΛ͏
const checkAnimal = (animal: string): Result<string, AnimalError> => { if
(animal === "Dogn") { return err({ kind: "DogError", message: "Invalid animal" }); } if (animal === "Catn") { return err({ kind: "CatError", message: "Invalid animal" }); } return ok("Animal"); };
݁ՌΛύλʔϯϚον
const result = checkAnimal("Dog"); result.match( (animal) => { console.log(animal); },
(error) => { switch (error.kind) { case "DogError": return `DogError: ${error.message}`; case "CatError": return `CatError: ${error.message}`; default: return assertNever(error); } } );
ΤϥʔΛཏͯ͠returnՄೳ
ଟ͗͢Δ߹
None
Γͳ͍߹
None
Honoʹ࠷దʂ
ͳͣͰͨ͠
ܕϑϩϯτʹ͢ͷΉͣ