Skip to content

assertNever

Asserts at compile time that every case of a discriminated union has been handled. Place at the default branch of a switch over a union or enum.

If a new variant is added to the union without a matching case, the call to assertNever fails to compile because value is no longer never. If reached at runtime (e.g. because the value was bypassed via JSON or a type assertion), it throws a descriptive Error.

Import:

import { assertNever } from 'obsidian-dev-utils/type-guards';

Example:

switch (mode) {
case 'a': return doA();
case 'b': return doB();
default: assertNever(mode);
}

Signature:

function assertNever(value: never): never

Parameters:

ParameterTypeDescription
valueneverThe exhaustively-handled value, narrowed to never by control flow.

Returns: never

Example:

switch (mode) {
case 'a': return doA();
case 'b': return doB();
default: assertNever(mode);
}