ts-type-forge
    Preparing search index...

    Type Alias BoundedLengthString<MinLength, MaxLength>

    Branded string type for strings whose length is between MinLength and MaxLength characters (both inclusive). Defined as the intersection of MinLengthString and MaxLengthString, so both bounds can be weakened independently: BoundedLengthString<M1, M2> is assignable to BoundedLengthString<N1, N2> if M1 >= N1 and M2 <= N2.

    Type Parameters

    • MinLength extends SupportedLength

      The minimum number of characters (inclusive). Must be a non-negative integer literal.

    • MaxLength extends SupportedLength

      The maximum number of characters (inclusive). Must be a non-negative integer literal.

    const userId = 'user-12345678' as BoundedLengthString<8, 16>;

    const relaxed: BoundedLengthString<1, 255> = userId; // OK ([8, 16] ⊆ [1, 255])
    const atLeast8: MinLengthString<8> = userId; // OK
    const atMost16: MaxLengthString<16> = userId; // OK
    // const strict: BoundedLengthString<10, 16> = userId; // Error! (8 < 10)