This function checks if a value is already an array, and if not, it wraps the value in an array.
JavaScript:
let castArray = val => Array.isArray(val) ? val : [val];
// Example
console.log(castArray('a')); // ['a']
console.log(castArray([1, 2, 3])); // [1, 2, 3]
TypeScript:
let castArray = (val: T | T[]): T[] => Array.isArray(val) ? val : [val];
// Example
console.log(castArray('a')); // ['a']
console.log(castArray([1, 2, 3])); // [1, 2, 3]