ts-data-forge
    Preparing search index...
    • Type guard that checks if a string has at most maxLength characters.

      The length is measured in UTF-16 code units (the same unit as String.prototype.length), so characters outside the Basic Multilingual Plane (e.g. emoji) count as 2.

      Type Narrowing Behavior:

      • Narrows the input to MaxLengthString<MaxLength> while preserving the original string type (e.g. literal types) via intersection.
      • The result participates in the length-constraint subtyping relation: for example, a value narrowed to MaxLengthString<16> is assignable to MaxLengthString<32>.

      Type Parameters

      • S extends string

        The input string type (literal types are preserved).

      • MaxLength extends ArgStr

        The maximum number of characters (inclusive).

      Parameters

      • s: S

        The string to check.

      • maxLength: MaxLength

        The maximum number of characters (inclusive).

      Returns s is string & Readonly<
          {
              MaxLength: RelaxedExclude<
                  IndexOfTupleImpl<
                      [...MakeTupleImpl<0, `${MaxLength}`, []>[], 0],
                      keyof [...MakeTupleImpl<0, `${MaxLength}`, []>[], 0],
                  >,
                  never,
              >;
          },
      > & Readonly<
          {
              "TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3": unknown;
          },
      > & S

      true if s.length <= maxLength, false otherwise. When true, TypeScript narrows s to MaxLengthString<MaxLength> & S.

      const input: string = 'noshiro';

      assert.isTrue(isMaxLengthString(input, 32));

      assert.isFalse(isMaxLengthString(input, 3));

      if (isMaxLengthString(input, 32)) {
      const userName: MaxLengthString<32> = input;

      const relaxed: MaxLengthString<64> = input; // OK (32 <= 64)
      }