Function hasPropertyValue

  • Checks if an object has the specified property with the given value as well as the type is the same.

    Example

    Property exists with matching value and type:

    const obj = { foo: 42, bar: "baz" };
    hasPropertyValue(obj, "foo", 42);
    // => { ok: true, value: 'true' }

    Example

    Property exists with non-matching value:

    const obj = { foo: 42, bar: "baz" };
    hasPropertyValue(obj, "foo", 0);
    // => { ok: true, value: 'false' }

    Example

    Property exists with matching type but different value type:

    const obj = { foo: 42, bar: "baz" };
    hasPropertyValue(obj, "foo", "42");
    // => { ok: true, value: 'false' }

    Example

    Property does not exist in the object:

    const obj = { foo: 42, bar: "baz" };
    hasPropertyValue(obj, "qux", 42);
    // => { ok: false, error: Error('[objects.hasPropertyValue] Property qux does not exist') }

    Type Parameters

    • T extends Record<string, any>

      The type of the object to check

    • K extends string | number | symbol

      The type of the property to check

    Parameters

    • obj: T

      The object to check

    • key: K

      The property key to check

    • value: any

      The value to compare against the property value

    Returns Result<boolean, Error>

    A Result object with either a boolean indicating whether the property exists and has the specified value and type, or an Error object if the property does not exist

Generated using TypeDoc v0.24.7