ts-data-forge
    Preparing search index...
    • Type guard that checks if a string's length is within the inclusive range [minLength, maxLength].

      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 BoundedLengthString<MinLength, MaxLength> while preserving the original string type (e.g. literal types) via intersection.
      • Since BoundedLengthString is the intersection of MinLengthString and MaxLengthString, the narrowed value is assignable to both, and both bounds can be weakened independently.

      Type Parameters

      • S extends string

        The input string type (literal types are preserved).

      • MinLength extends ArgStr

        The minimum number of characters (inclusive).

      • MaxLength extends ArgStr

        The maximum number of characters (inclusive).

      Parameters

      • s: S

        The string to check.

      • minLength: MinLength

        The minimum number of characters (inclusive).

      • 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;
          },
      > & Readonly<
          { MinLength: readonly [MakeTupleImpl<0, `${MinLength}`, []>, 0] },
      > & S

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

      const input: string = 'user-12345678';

      assert.isTrue(isBoundedLengthString(input, 8, 16));

      assert.isFalse(isBoundedLengthString('user', 8, 16));

      if (isBoundedLengthString(input, 8, 16)) {
      const userId: BoundedLengthString<8, 16> = input;

      const relaxed: BoundedLengthString<1, 255> = input; // OK ([8, 16] ⊆ [1, 255])
      }