Flatten Nested Arrays One Level, Typed

Write the code, see it run, pass the checks.

The problem

  • easy
  • Code

Export a type ElementType<T>: the element type when T is an array (read-only arrays too), otherwise T itself. Then write flattenOnce(items): return a new array in which every array item is replaced by its elements, one level deep only, keeping the order. Type the result as an array of ElementType of the items, so [[1, 2], [3]] gives number[] and [1, ['a']] gives (number | string)[].

Your code must pass

  • Flattens one level only
  • Keeps order and leaves the input unchanged
  • Has no type errors
  • ElementType unwraps one level of array
  • flattenOnce returns the element type