ts-data-forge
    Preparing search index...
    • Type guard that checks if a string has exactly length 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 FixedLengthString<Length> while preserving the original string type (e.g. literal types) via intersection.
      • Since FixedLengthString<Length> is defined as BoundedLengthString<Length, Length>, the narrowed value is assignable to any MinLengthString<N> with N <= Length and any MaxLengthString<N> with N >= Length.

      Type Parameters

      • S extends string

        The input string type (literal types are preserved).

      • Length extends ArgStr

        The exact number of characters.

      Parameters

      • s: S

        The string to check.

      • length: Length

        The exact number of characters.

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

      true if s.length === length, false otherwise. When true, TypeScript narrows s to FixedLengthString<Length> & S.

      const input: string = 'JP';

      assert.isTrue(isFixedLengthString(input, 2));

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

      if (isFixedLengthString(input, 2)) {
      const countryCode: FixedLengthString<2> = input;

      const atMost5: MaxLengthString<5> = input; // OK (2 <= 5)
      }