ts-data-forge
    Preparing search index...

    Function asNonEmptyString

    • Casts a string to NonEmptyString after checking that it has at least one character.

      NonEmptyString is an alias of MinLengthString<1>, so this is the length-1 specialization of asMinLengthString. Use this instead of writing s as unknown as NonEmptyString: the length is verified at runtime, and the original string type (e.g. literal types) is preserved via intersection.

      Type Parameters

      • S extends string

        The input string type (literal types are preserved).

      Parameters

      • s: S

        The string to cast.

      Returns string & Readonly<{ MinLength: readonly [0, 0] }> & Readonly<
          {
              "TSTypeForgeInternals--edd2f9ce-7ca5-45b0-9d1a-bd61b9b5d9c3": unknown;
          },
      > & S

      s typed as NonEmptyString & S.

      const name = Str.asNonEmptyString('noshiro');

      const nonEmpty: NonEmptyString = name; // OK

      assert.isTrue(nonEmpty.length >= 1);

      assert.throws(() => Str.asNonEmptyString('')); // length 0 < 1

      If s is empty.