A non-negative integer literal type to decrement.
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;
Decrements a positive integer literal type
Nby 1.This utility performs compile-time arithmetic by leveraging TypeScript's tuple manipulation. It creates a tuple of length
N, removes the first element usingList.Tail, and returns the new length type. This effectively computesN - 1at the type level.Note:
Decrement<0>does not error; it clamps to0, becauseList.Tailof an empty tuple is an empty tuple (whose length is0).