Back to questions
JuniorTheoryCommon
What does tsc report for this file?
type Pay = "card" | "cash" | "crypto";
function payName(p: Pay): string {
switch (p) {
case "card":
return "Card";
case "cash":
return "Cash";
default:
const rest: never = p;
return rest;
}
}An error: "crypto" does not fit the type never
- Expecting the
neverline to be a run-time check: tsc checks it, and a forgotten member makes the file fail to compile. - Expecting the error on the
typeline: a union may have any members, and tsc reports the line where the unhandled"crypto"is assigned tonever.
- How would you fix
payNameso that the file compiles? - Why is
return rest;allowed in a function that returnsstring?