ts-type-forge
    Preparing search index...
    Increment: (readonly [0, ...MakeTuple<0, N>])["length"]

    Increments a non-negative integer literal type N by 1.

    This utility performs compile-time arithmetic by leveraging TypeScript's tuple length calculation. It creates a tuple of length N, prepends an element, and returns the new length type. This effectively computes N + 1 at the type level.

    Note: Due to TypeScript's recursion limits, this works reliably for small to medium integers (typically up to a few hundred, depending on the TypeScript version and configuration).

    Type Parameters

    • N extends number

      A non-negative integer literal type to increment.

    The number literal type representing N + 1.

    type Five = Increment<4>;  // 5
    type One = Increment<0>; // 1
    type Ten = Increment<9>; // 10

    // Useful in recursive type computations
    type CountToN<N extends number, Count extends number = 0> =
    Count extends N ? Count : CountToN<N, Increment<Count>>;

    type UpTo5 = CountToN<5>; // 5

    // Building sequences
    type Range<From extends number, To extends number> =
    From extends To
    ? From
    : From | Range<Increment<From>, To>;

    type OneToFive = Range<1, 5>; // 1 | 2 | 3 | 4 | 5