Skip to content
webtype.orgwebtype.org#218 last-element · par 3

    ↑↓ move · ⏎ open · esc close

    No. 218 · July 26, 2026 · Moderate

    Last

    Implement `Last<T>` so it returns the type of the final element of a tuple. An empty tuple has no last element — return `never`.

    01

    Try the puzzle yourself

    Par 3

    Puzzle

    last-element.ts
    Stroke 1 of 3Not run yet

    Replace ??? — your solution is checked against the cases below. Tab indents; press Escape then Tab to move focus out.

    Checks

    4
    • Last<[1, 2, 3]>
      3
    • Last<['only']>
      'only'
    • Last<[]>
      never
    • Last<[string, number]>
      number

    How a check is judged Exact type equality, not assignability — an intersection is not the same as the flattened object.

    How everyone did

    Fewer than 5 people have solved this one so far. The distribution appears once there is enough of a sample to mean anything.

    Short game

    Fewest characters

    No public scores yet.

    Archive
    02

    Annotated solution

    Published July 27, 2026

    The solution

    type Last<T extends readonly unknown[]> =
      T extends readonly [...unknown[], infer L] ? L : never

    The common wrong answer

    type Last<T extends readonly unknown[]> = T[number]

    Indexing a tuple with `number` gives the union of *every* element type, so `[1, 2, 3]` yields `1 | 2 | 3`. Position information is lost the moment you index that way.

    Line by line

    1. [...unknown[], infer L]

      A tuple pattern may contain exactly one rest element, and it is allowed to sit at the front. Matched against a fixed-length tuple, the rest absorbs everything except the final slot, which binds to `L`.

    2. : never

      The empty tuple matches nothing — there is no final slot to bind — so the false branch answers with the type that has no values.

    Takeaway

    A rest element in a tuple pattern does not have to be last. Leading rests are how you reach the end of a tuple without counting your way there.

    Uses