Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "utils/safe-prop"

Index

Functions

Functions

safePropAccess

  • safePropAccess<T, P>(obj: T, name: P, callback: function): void
  • Tests if the given property of an object is available, and if so passes the property value to the given callback.

    Remarks: A possible bug in TypeScript is that even though we null check the property value before passing it to the callback, if TypeScript deduces the type of the property to be in the domain of null | undefined, that such constraints still flow through to the callback function parameter.

    Consider this interface:

    example

    interface Foo { name: string; bar: () => void | undefined; } let o: Foo = ...; safePropAccess(o, "bar", func => { //TypeScript still thinks func is [() => void | undefined] even though safePropGet tests for null/undefined //before passing. To workaround this, use the ! operator to override and tell TypeScript that "func" cannot //possibly be null or undefined func!(); });

    since

    0.11

    Type parameters

    • T

    • P: keyof T

    Parameters

    • obj: T

      The object

    • name: P

      The name of the property

    • callback: function

      The callback that will receive the property value

        • (value: T[P]): void
        • Parameters

          • value: T[P]

          Returns void

    Returns void

Generated using TypeDoc