Skip to content

Latest commit

 

History

History
81 lines (74 loc) · 2.84 KB

type-guarantee.md

File metadata and controls

81 lines (74 loc) · 2.84 KB

Type Guarantee

A type guarantee is a function that throws an Error if the input doesn't have a compatible type. We call them to improve the robustness of our code. They're a form of data guarantee.

For example:

export function mustBeValue(
    input: unknown,
    onError: OnError
): input is Value<T> {
    if (isValue(input)) {
        return true;
    }

    throw onError(new TypeError("input is not a value!!));
}

The key properties of a type guarantee are:

  • it is asserting that an unknown input is a specific type (which can include union types)
  • there's no return value, so it doesn't matter if the caller forgets to check it