次のページでは、遷移関数の引数を CheckedIn などの状態の型に絞ります。すると、Appointment を持つ呼び出し側は、kind を検証して型を絞り込んでから渡す必要があります。このページでは、その絞り込みの書き方を確認します。
Union は、値が複数の型のうちのどれか 1 つであることを表す型です。
-
説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82type 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どちらの型も持つ -
veterinarianIdInExamination だけが持つ
CheckedIn と InExamination は、どちらも kind を持ちます。kind の値は、型ごとに違う文字列です。このように共通の kind を持つ Union を、Discriminated Union と呼びます。
-
-
説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82type 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 のどちらかです。この時点では、どちらの型か判別できないため、両方が持つ項目だけを使えます。
-
-
説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82type 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 の行そのものでは、まだ型は絞られていません。
-
-
説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82type 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 の項目だけを使えます。
-
-
説明用の短い例。実物は examples/session-01/src/domain/appointment/appointment.ts:76-82type 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 を判定するだけで、使える項目が変わります。
-