Function isObject

  • Checks if a value is typeof object and not null or an array.

    Type Parameters

    • T

    Parameters

    • value: T

      The value to check.

    Returns value is T extends Function | unknown[]
        ? never
        : T & object

    true if the check passes, false otherwise.

    Remarks

    This function will return false for an array. If you do not want this behavior, use isObjectLoose instead.

    Example

    Basic usage

    isObject({}) // true
    isObject([]) // false
    isObject(null) // false

    Type narrowing

    const value: { foo: string } | string[]

    if (isObject(value)) {
    value // type: { foo: string }
    } else {
    value // type: string[]
    }

    // works with function too
    const value: { foo: string } | (() => string)

    if (isObject(value)) {
    value // type: { foo: string }
    value.foo // ok
    } else {
    value // (() => string)
    value() // ok
    }

    Paired with assert

    import { assert } from 'uft'

    const options: { opt: string } | string[]
    assert(isObject(options), 'expected options object')
    options // type: { opt: string }

Generated using TypeDoc