0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

題名通り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パターン書く必要ありませんね〜

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?