Back to questionsWhich type does
JuniorTheoryCommon
Which type does s have on the line return "paid: " + s.total;?
type OrderState =
| { kind: "new" }
| { kind: "paid"; total: number }
| { kind: "shipped"; track: string };
function statusText(s: OrderState): string {
switch (s.kind) {
case "paid":
return "paid: " + s.total;
}
return "other";
}{ kind: "paid"; total: number; }
- Expecting
sto keep its declared typeOrderState: thecasecompares the discriminant, and inside itsis the paid variant withtotal. - Confusing
swiths.kind: the check narrows the whole object, sosis{ kind: "paid"; total: number; }, not"paid".
- Which type does
shave after theswitch, on the linereturn "other";? - How would you make tsc report a state that the
switchforgot?