TypeAssert

Util.TypeAssert()
TypeAssert(_x: T): void

This purpose of this function is guarantee a value is of a given type.

This is essentially a compile time console.assert(x instanceof T).

This function is useful when terminating a type-narrowing if-else chain in that this function makes the code more robust to future type changes.

Warning:

T=never doesn’t appear to work properly, hence the existence of TypeAssertNever.

Example:

declare const x: Foo | Bar;
if (x instanceof Foo) {
    console.log("foo");
}
else {
    // This becomes a compiler error if, for example, `x`'s type gets changed to add `Baz`:
    //    x: Foo | Bar | Baz
    TypeAssert<Bar>(x);
    console.log("bar");
}

Parameters

_x: T

Returns: void