ts-type-forge
    Preparing search index...

    Type Alias Decrement<N>

    Decrement: List.Tail<MakeTuple<0, N>>["length"]

    Decrements a positive integer literal type N by 1.

    This utility performs compile-time arithmetic by leveraging TypeScript's tuple manipulation. It creates a tuple of length N, removes the first element using List.Tail, and returns the new length type. This effectively computes N - 1 at the type level.

    Note: Decrement<0> does not error; it clamps to 0, because List.Tail of an empty tuple is an empty tuple (whose length is 0).

    Type Parameters

    • N extends number

      A non-negative integer literal type to decrement.

    The number literal type representing N - 1 (or 0 for N = 0).

    type Three = Decrement<4>; // 3
    type Zero = Decrement<1>; // 0
    type Four = Decrement<5>; // 4

    // Note: `Decrement<0>` does not error; it clamps to 0
    // (`List.Tail` of an empty tuple is an empty tuple, whose length is 0).
    type ClampedAtZero = Decrement<0>; // 0

    // Useful in countdown scenarios
    type Countdown<N extends number> = N extends 0
    ? 0
    : N | Countdown<Decrement<N>>;

    type CountdownFrom3 = Countdown<3>; // 3 | 2 | 1 | 0

    // Bounds checking
    type IsPositive<N extends number> = N extends 0
    ? false
    : N extends Decrement<Increment<N> & number>
    ? true
    : false;