hasInstance<T, K, C>(value, key, ctor): value is T & View<K, InstanceType<C>>
Tests whether a value of type T contains a property key whose type tests positively when tested with
isInstance when compared with the given constructor type C. If so, the type of the tested value is
narrowed to reflect the existence of that key for convenient access in the same scope. Returns false if the
property key does not exist on the object or the value stored by that key is not an instance of C.
classServerResponse { ... } // type of obj -> unknown if (hasNumber(obj, 'status')) { // type of obj -> { status: number } if (hasInstance(obj, 'data', ServerResponse)) { // type of obj -> { status: number } & { data: ServerResponse } } elseif (hasString('error')) { // type of obj -> { status: number } & { error: string } } }
Tests whether a value of type
T
contains a propertykey
whose type tests positively when tested with isInstance when compared with the given constructor typeC
. If so, the type of the tested value is narrowed to reflect the existence of that key for convenient access in the same scope. Returnsfalse
if the property key does not exist on the object or the value stored by that key is not an instance ofC
.