• Tests whether a value of type T contains one or more property keys. If so, the type of the tested value is narrowed to reflect the existence of those keys for convenient access in the same scope. Returns false if the property key does not exist on the target type, which must be an object. Returns true if the property key exists, even if the associated value is undefined or null.

    // type of obj -> unknown
    if (has(obj, 'name')) {
    // type of obj -> { name: unknown }
    if (has(obj, 'data')) {
    // type of obj -> { name: unknown } & { data: unknown }
    } else if (has(obj, ['error', 'status'])) {
    // type of obj -> { name: unknown } & { error: unknown, status: unknown }
    }
    }

    Type Parameters

    • T
    • K extends string

    Parameters

    • value: T

      The value to test.

    • keys: Many<K>

      One or more string keys to check for existence.

    Returns value is T & object & View<K>