assertNever
A one-line helper that makes a `switch` over a discriminated union a compile error the moment a case is added and left unhandled, with a runtime throw as the safety net for data that violated the types at the edges.
2 min read
#toolingIn the default branch of a switch over a discriminated union,
#typescript narrows the value down
to never - but only once every other case is handled. Pass it to a
function typed (x: never) and that guarantee becomes load-bearing:
add a variant, miss a case, and the code stops compiling.
@itsnas TypeScript
main
Nas (@itsnas)