TypeScript 7 · Utility types and narrowing
Check a switch handles every case
Make the compiler fail if a union member is not handled.
function label(status: Status): string {
switch (status) {
case "idle": return "Idle";
case "loading": return "Loading";
case "error": return "Error";
default: {
const unreachable: never = status;
return unreachable;
}
}
}
Official TypeScript 7 documentation
More utility types and narrowing commands
- Make all properties optional
- Pick or omit properties
- Type a dictionary
- Require or freeze properties
- Remove null and undefined
- Write a type guard
All utility types and narrowing commands · TypeScript 7 cheatsheet