ts-type-forge
    Preparing search index...
    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.

    Warning: This requires N to be positive (>= 1). Attempting to decrement 0 will likely result in compilation errors or unexpected behavior.

    Type Parameters

    • N extends number

      A positive integer literal type (must be >= 1) to decrement.

    The number literal type representing N - 1.

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

    // type Error = Decrement<0>; // ⚠️ Error: will fail or return unexpected result

    // 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>> ? true : false;