• Tests whether a value of type T contains a property key of type string. 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 of type string.

    // type of obj -> unknown
    if (hasString(obj, 'name')) {
    // type of obj -> { name: string }
    if (hasString(obj, 'message')) {
    // type of obj -> { name: string } & { message: string }
    }
    }

    Type Parameters

    • T
    • K extends string

    Parameters

    • value: T

      The value to test.

    • key: K

    Returns value is T & object & View<K, string>