Options
All
  • Public
  • Public/Protected
  • All
Menu

This module contains functions to fail-fast on null/undefined, and do various type guards and narrowing in TypeScript.

Index

Functions

  • checkNotNull<T>(o: undefined | null | T, message?: string): T
  • Alias for checkNotUndefined

    Type parameters

    • T

    Parameters

    • o: undefined | null | T
    • Optional message: string

    Returns T

  • checkNotUndefined<T>(o: undefined | null | T, message?: string): T
  • Shorthand notation for converting an unexpected null/undefined reference to a more telling error message.

    E.g const mapElement = checkNotNull(document.getElementById('map'), 'Missing #map element in DOM'); will result in a strongly-typed HTMLElement which cannot be null, or throw an Error with a dedicated message (rather than simply crashing with a null-pointer reference when used later).

    throws

    If the object was null or undefined

    Type parameters

    • T

    Parameters

    • o: undefined | null | T

      An object of a union type including null and undefined.

    • Optional message: string

      An optional message to throw as Error message.

    Returns T

    A non-null object

  • isNotNull<T>(o: undefined | null | T): o is T
  • Alias for isNotUndefined

    Type parameters

    • T

    Parameters

    • o: undefined | null | T

    Returns o is T

  • isNotUndefined<T>(o: undefined | null | T): o is T
  • A type guard which uses generics to narrow a "type or null or undefined" to "type". This is particularly useful with RxJs filter, as it recognizes the narrowing and infers the type of the resulting stream.

    Type parameters

    • T

    Parameters

    • o: undefined | null | T

    Returns o is T

  • nullSafe<T, U>(func: (t: T) => U): (t: T | undefined | null) => U | undefined
  • Alias for undefinedSafe

    Type parameters

    • T

    • U

    Parameters

    • func: (t: T) => U
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns (t: T | undefined | null) => U | undefined

      • (t: T | undefined | null): U | undefined
      • Converts the given function to a new function which calls through to the original function except if the input is undefined, in which case the function returns undefined. In other words, it wraps a function to guard it against undefined input.

        Parameters

        • t: T | undefined | null

        Returns U | undefined

  • trimEmptyToUndefined(s: undefined | null | string): string | undefined
  • Returns a trimmed string, or undefined if the string ends up empty.

    Parameters

    • s: undefined | null | string

    Returns string | undefined

  • trimUndefinedToEmpty(s: undefined | null | string): string
  • Returns a trimmed string, converting undefined to an empty string.

    Parameters

    • s: undefined | null | string

    Returns string

  • trimmedEmptyToUndefined(s: undefined | null | string): string | undefined
  • deprecated
    see

    trimEmptyToUndefined Legacy name

    Parameters

    • s: undefined | null | string

    Returns string | undefined

  • trimmedUndefinedToEmpty(s: undefined | null | string): string
  • deprecated
    see

    trimUndefinedToEmpty Legacy name

    Parameters

    • s: undefined | null | string

    Returns string

  • undefinedSafe<T, U>(func: (t: T) => U): (t: T | undefined | null) => U | undefined
  • Converts the given function to a new function which calls through to the original function except if the input is undefined, in which case the function returns undefined. In other words, it wraps a function to guard it against undefined input.

    Type parameters

    • T

    • U

    Parameters

    • func: (t: T) => U
        • (t: T): U
        • Parameters

          • t: T

          Returns U

    Returns (t: T | undefined | null) => U | undefined

      • (t: T | undefined | null): U | undefined
      • Converts the given function to a new function which calls through to the original function except if the input is undefined, in which case the function returns undefined. In other words, it wraps a function to guard it against undefined input.

        Parameters

        • t: T | undefined | null

        Returns U | undefined