Function toArray

  • Converts a value to an array it is not already one.

    Type Parameters

    • T

    Parameters

    • maybeArray: T

      The value to convert.

    Returns T[]

    An array containing the value or the value itself if it was already an array.

    Example

    Basic Usage

    const a = toArray(1) // type: number[]
    console.log(a) // [1]

    const b = toArray([1, 2, 3]) // type: number[]
    console.log(b) // [1, 2, 3]

    const itemOrItems: string | string[]
    const c = toArray(itemOrItems) // type: string[]

    If the value is a union of different types, the return type is still a single array.

    const strOrNum: string | number
    const c = toArray(strOrNum) // type: (string | number)[]
    c.push('hi')
    c.push(1)

Generated using TypeDoc