Skip to content

castArray() function

Casts value as an array if it's not one. If value is already an array, the same reference is returned as-is. null and undefined are wrapped like any other value.

Signature:

typescript
declare function castArray<T>(value: T | T[]): T[];

Example

ts
castArray(1); // [1]
castArray("a"); // ["a"]
castArray({ a: 1 }); // [{ a: 1 }]
castArray(null); // [null]
castArray(undefined); // [undefined]

const arr = [1, 2, 3];
castArray(arr) === arr; // true - same reference

Open Source Code

Released under the Apache-2.0 License.