0 of 6 problems solved0%
4 of 6

Single or Many

Sometimes a function should accept either a single item or an array of items. Use Array.isArray to decide which one you have and normalize to one consistent shape.

function ensureArray(s: string | string[]) {
  return Array.isArray(s) ? s : [s];
}

Return a new array every time so callers can safely reuse the result.

Your Task

Normalize input into an array of strings.

  • Write function asList(v: string | string[]): string[].
  • If v is a string, return [v].
  • If v is an array, return a new array with the same elements (do not return the input reference).
  • Do not mutate the input.