はじめに
題名通りts-patternで同じ結果の場合分けがしたい時の方法です。
対応方法
Pを活用しましょう!
import { P, match } from "ts-pattern";
type Group = "A" | "B" | "C";
const groupResult = (group: Group): string => {
return match(group)
.with("A", () => "GroupA")
.with(P.union("B", "C"), () => "other Group")
.exhaustive();
}
さいごに
これで同じものを2パターン書く必要ありませんね〜