ホーム 全セッション S01

共通の kind で型を絞り込む

5 / 10 目安 1.5分

ページ一覧

事前知識: 引数を絞ると呼び出し側が kind を検証する必要があるため、kind で型を絞り込む書き方を学びます。

次のページでは、遷移関数の引数を CheckedIn などの状態の型に絞ります。すると、Appointment を持つ呼び出し側は、kind を検証して型を絞り込んでから渡す必要があります。このページでは、その絞り込みの書き方を確認します。

Union は、値が複数の型のうちのどれか 1 つであることを表す型です。

  1. 説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82
    type CheckedIn = Readonly<{ kind: "CheckedIn"; checkedInAt: string }>;
    type InExamination = Readonly<{
      kind: "InExamination";
      checkedInAt: string;
      veterinarianId: string;
    }>;
    type Appointment = CheckedIn | InExamination;
    
    const describe = (appointment: Appointment): string => {
      switch (appointment.kind) {
        case "CheckedIn":
          return appointment.checkedInAt;
        case "InExamination":
          return appointment.veterinarianId;
      }
    };

    appointment の型: 2つの型が持つ項目

    • kind どちらの型も持つ。値は型ごとに違う
    • checkedInAt どちらの型も持つ
    • veterinarianId InExamination だけが持つ

    CheckedIn と InExamination は、どちらも kind を持ちます。kind の値は、型ごとに違う文字列です。このように共通の kind を持つ Union を、Discriminated Union と呼びます。

  2. 説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82
    type CheckedIn = Readonly<{ kind: "CheckedIn"; checkedInAt: string }>;
    type InExamination = Readonly<{
      kind: "InExamination";
      checkedInAt: string;
      veterinarianId: string;
    }>;
    type Appointment = CheckedIn | InExamination;
    
    const describe = (appointment: Appointment): string => {
      switch (appointment.kind) {
        case "CheckedIn":
          return appointment.checkedInAt;
        case "InExamination":
          return appointment.veterinarianId;
      }
    };

    appointment の型: Appointment

    • kind 使える
    • checkedInAt 使える
    • veterinarianId 使えない。どちらの型か判別できない

    Appointment は、CheckedIn か InExamination のどちらかです。この時点では、どちらの型か判別できないため、両方が持つ項目だけを使えます。

  3. 説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82
    type CheckedIn = Readonly<{ kind: "CheckedIn"; checkedInAt: string }>;
    type InExamination = Readonly<{
      kind: "InExamination";
      checkedInAt: string;
      veterinarianId: string;
    }>;
    type Appointment = CheckedIn | InExamination;
    
    const describe = (appointment: Appointment): string => {
      switch (appointment.kind) {
        case "CheckedIn":
          return appointment.checkedInAt;
        case "InExamination":
          return appointment.veterinarianId;
      }
    };

    appointment の型: Appointment

    • kind 使える
    • checkedInAt 使える
    • veterinarianId 使えない。どちらの型か判別できない

    switch は、kind の値で場合分けします。switch の行そのものでは、まだ型は絞られていません。

  4. 説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82
    type CheckedIn = Readonly<{ kind: "CheckedIn"; checkedInAt: string }>;
    type InExamination = Readonly<{
      kind: "InExamination";
      checkedInAt: string;
      veterinarianId: string;
    }>;
    type Appointment = CheckedIn | InExamination;
    
    const describe = (appointment: Appointment): string => {
      switch (appointment.kind) {
        case "CheckedIn":
          return appointment.checkedInAt;
        case "InExamination":
          return appointment.veterinarianId;
      }
    };

    appointment の型: CheckedIn

    • kind 使える
    • checkedInAt 使える
    • veterinarianId 使えない。CheckedIn にはない

    case "CheckedIn" の中では、appointment の型が CheckedIn に絞られます。これを型の絞り込みと呼びます。CheckedIn の項目だけを使えます。

  5. 説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82
    type CheckedIn = Readonly<{ kind: "CheckedIn"; checkedInAt: string }>;
    type InExamination = Readonly<{
      kind: "InExamination";
      checkedInAt: string;
      veterinarianId: string;
    }>;
    type Appointment = CheckedIn | InExamination;
    
    const describe = (appointment: Appointment): string => {
      switch (appointment.kind) {
        case "CheckedIn":
          return appointment.checkedInAt;
        case "InExamination":
          return appointment.veterinarianId;
      }
    };

    appointment の型: InExamination

    • kind 使える
    • checkedInAt 使える
    • veterinarianId 使える

    case "InExamination" の中では、InExamination に絞られるので、veterinarianId を使えます。kind を判定するだけで、使える項目が変わります。