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)
Branded string type for strings with at most
MaxLengthcharacters.The brand is encoded so that the natural subtyping relation between length constraints is preserved: if
M <= N, thenMaxLengthString<M>is assignable toMaxLengthString<N>(a string of at mostMcharacters is also a string of at mostNcharacters). This is achieved by branding with the union of allowed lengths (0 | 1 | ... | MaxLength), which shrinks as the constraint gets stricter.