has<T, K>(value, keys): value is T & object & View<K>
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 } } elseif (has(obj, ['error', 'status'])) { // type of obj -> { name: unknown } & { error: unknown, status: unknown } } }
Tests whether a value of type
T
contains one or more propertykeys
. 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 isundefined
ornull
.