ts-type-forge
    Preparing search index...

    Type Alias WithSmallInt<N, MaxIndex>

    WithSmallInt: WithSmallIntImpl<CastToInt<NormalizeBrandUnion<N>>, MaxIndex>

    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.

    Type Parameters

    • N extends Int

      The integer brand type to enhance

    • MaxIndex extends number = TSTypeForgeInternals_SmallIntIndexMax

      Maximum absolute value for literals (default: 40)

    Union of literal integers and the branded type

    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