Skip to main content

Cast a value as an array

JavaScript version

const castArray = (value) => (Array.isArray(value) ? value : [value]);

TypeScript version

const castArray = <T,_>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);

Examples

castArray(1); // [1]
castArray([1, 2, 3]); // [1, 2, 3]

Comments