Skip to content
webtype.orgVariadic tuples

    ↑↓ move · ⏎ open · esc close

    Constructs

    Variadic tuples

    Spreading and matching tuples by position, which is how the type level does lists.

    What it is

    A tuple knows its own length as a literal type, which is the fact every counting type is built on. Combine that with head-and-rest patterns and you have recursion over a list: peel one element, do something, recurse on what remains. Parameter lists are tuples too, which is why the same techniques rewrite function signatures.

    Examples

    • Push<[1, 2], 3>
      [1, 2, 3]
    • Tail<[1, 2, 3]>
      [2, 3]
    • ['a', 'b']['length']
      2

      The literal length. An array would answer `number` here, and that difference is the whole reason tuples matter at the type level.

    Each resolved type above was printed by TypeScript 5.9.3, not written by hand.

    What it does not do

    • It does not let you index past the end. A tuple’s length is a promise, and TS2493 is the compiler keeping it.
    • It does not recurse indefinitely. Counting one element at a time meets the instantiation ceiling around fifty, which is TS2589 and is not tunable.

    Takeaway

    When you need a number at the type level, you almost always want a tuple you grow and then ask for its length. The contents are never read.