• Use the Fail function as a template literal tag to efficiently create and throw a details-style error only when a condition is not satisfied.

    condition || Fail`...complaint...`;
    

    This avoids the overhead of creating usually-unnecessary errors like

    assert(condition, details`...complaint...`);
    

    while improving readability over alternatives like

    condition || assert.fail(details`...complaint...`);
    

    However, due to current weakness in TypeScript, static reasoning is less powerful with the || patterns than with an assert call. Until/unless https://github.com/microsoft/TypeScript/issues/51426 is fixed, for ||-style assertions where this loss of static reasoning is a problem, instead express the assertion as

      if (!condition) {
    Fail`...complaint...`;
    }

    or, if needed,

      if (!condition) {
    // `throw` is noop since `Fail` throws, but it improves static analysis
    throw Fail`...complaint...`;
    }

    Parameters

    • template: string[] | TemplateStringsArray
    • Rest...args: any

    Returns never