The integer brand type to enhance
Maximum absolute value for literals (default: 40)
type Count = WithSmallInt<Uint>;
// Count is 0 | 1 | 2 | ... | 39 | Uint
const increment = (n: Count): Count => {
if (typeof n === 'number' && n < 39) {
return (n + 1) as Count; // Type narrowing works with literals
}
return (n as number + 1) as Count;
};
// Common patterns:
type SmallInt = WithSmallInt<Int>; // -40 to 39 | Int
type SmallUint = WithSmallInt<Uint>; // 0 to 39 | Uint
type SmallPositiveInt = WithSmallInt<PositiveInt>; // 1 to 39 | PositiveInt
Enhances an integer brand type with literal values for small integers. This enables more precise typing for small integer values while maintaining the brand for larger values.