ts-type-forge
    Preparing search index...
    MaxLengthString: string & TSTypeForgeInternals_BrandEncapsulated<
        Readonly<{ MaxLength: UintRangeInclusive<0, MaxLength> }>,
    >

    Branded string type for strings with at most MaxLength characters.

    The brand is encoded so that the natural subtyping relation between length constraints is preserved: if M <= N, then MaxLengthString<M> is assignable to MaxLengthString<N> (a string of at most M characters is also a string of at most N characters). This is achieved by branding with the union of allowed lengths (0 | 1 | ... | MaxLength), which shrinks as the constraint gets stricter.

    Type Parameters

    • MaxLength extends SupportedLength

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

    const isMaxLengthString = <N extends SupportedLength>(
    s: string,
    maxLength: N,
    ): s is MaxLengthString<N> => s.length <= maxLength;

    const userName = 'noshiro' as MaxLengthString<32>;

    const short: MaxLengthString<64> = userName; // OK (32 <= 64)
    // const tooLong: MaxLengthString<16> = userName; // Error! (32 > 16)