• 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.

    class ServerResponse { ... }
    // 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 }
    } else if (hasString('error')) {
    // type of obj -> { status: number } & { error: string }
    }
    }

    Type Parameters

    Parameters

    Returns value is T & View<K, InstanceType<C>>