Skip to content
webtype.orgwebtype.org

    ↑↓ move · ⏎ open · esc close

    No. 257 · September 9, 2026 · Moderate

    Tuple to object

    Implement TupleToObject<T> so a tuple of literal keys becomes an object mapping each of them to itself.

    01

    Try the puzzle yourself

    Par 3

    Puzzle

    tuple-to-object.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

    3
    • TupleToObject<['a', 'b']>—
      → { a: 'a'; b: 'b' }
    • TupleToObject<['x']>—
      → { x: 'x' }
    • TupleToObject<[]>—
      → {}

    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 September 10, 2026

    The solution

    type TupleToObject<T extends readonly PropertyKey[]> = { [K in T[number]]: K }

    The common wrong answer

    type TupleToObject<T extends readonly PropertyKey[]> = { [K in keyof T]: T[K] }

    Mapping over keyof T when T is a tuple gives you a *tuple* back, not an object — the mapping is homomorphic, so it preserves the arrayness along with everything else. ["a", "b"] maps to ["a", "b"], which is a perfectly good answer to a question nobody asked.

    Line by line

    1. T[number]

      Indexing a tuple by number asks for "the type at any index", which is the union of everything in it: "a" | "b". That union is the key set you actually want to iterate.

    2. { [K in T[number]]: K }

      Because the key set is computed rather than keyof T, the mapping is no longer homomorphic — and that is exactly why the result comes back as a plain object.

    Takeaway

    keyof T on a tuple means its indices; T[number] means its contents. Reaching for the wrong one turns an object into a tuple, or the other way round, and the error message rarely says so.

    Uses