Slide 12
Slide 12 text
複雑な条件分岐もReadableに書ける - ts-pattern
処理を追加する場合を想像
💭
ts-patternで書かれている方が改修しやすいはず…!
const price = (order: Order): number => {
const [drink, size] = order;
switch (drink) {
case "coffee":
switch (size) {
case "small":
return 100;
case "medium":
return 200;
case "large":
return 300;
}
case "tea":
switch (size) {
case "small":
return 150;
case "medium":
return 250;
case "large":
return 350;
}
}
}
type Drink = "coffee" | "tea" | "soda";
type Size = "small" | "medium" | "large";
type Order = [Drink, Size];
const price = (order: Order): number => {
return match(order)
.with(["coffee", "small"], () => 100)
.with(["coffee", "medium"], () => 200)
.with(["coffee", "large"], () => 300)
.with(["tea", "small"], () => 150)
.with(["tea", "medium"], () => 250)
.with(["tea", "large"], () => 350)
.exhaustive();
}