Skip to content

@technobuddha > library > Object > Conversion

Function: toArray()

ts
function toArray<T>(value: T | T[]): T[];

Defined in: to-array.ts:16

Ensures that the provided value is returned as an array.

If the value is already an array, it is returned as-is. Otherwise, the value is wrapped in a new array.

Type Parameters

Type ParameterDescription
TThe type of the value or array elements.

Parameters

ParameterTypeDescription
valueT | T[]The value or array to convert to an array.

Returns

T[]

An array containing the value(s).

Example

ts
toArray(5); // [5]
toArray([1, 2, 3]); // [1, 2, 3]