Command Palette

Search for a command to run...

Hectal
Phase 17: Interviews and Capstone
TypeScript · Exercise 2 of 6 PreviousNext
Intermediate

Challenge: TupleToUnion and Last

Write TupleToUnion<T> (the union of a tuple's element types) and Last<T> (the type of its last element, or never for an empty tuple).

TupleToUnion<['a', 'b', 3]> is 'a' | 'b' | 3. Last<[1, 2, 3]> is 3, and Last<[]> is never. TupleToUnion must also accept readonly tuples, and must reject something that is not an array, like string.

What Check tests

  1. Your code has no type errors
  2. TupleToUnion accepts a readonly tuple
  3. TupleToUnion rejects a type that is not an array
  4. Last works with one element

Stuck? One hint at a time

The solution unlocks after your first Check. Try first: that is where the learning happens.

Runs in your browser. Your code is saved here.

Console

Nothing printed yet.