In TypeScript, a Record
type used with an enum for keys is exhaustively checked. That means if you forget an enum variant in the Record
it’s a compile-time error.
enum Status {
Todo,
InProgress,
Done,
}
const StatusIcon: Record<Status, string> = {
[Status.Todo]: "todo.svg",
[Status.InProgress]: "in-progress.svg",
[Status.Done]: "done.svg",
}
See also:
- Static types make it easier to work on projects sporadically
- Exhaustiveness checking helps TypeScript pass the airplane test