uft
    Preparing search index...

    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 extends readonly U[] ? U : T)[]

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

      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)