API reference › @evolu/common › Type › endsWith

function endsWith<Suffix extends string>(
  suffix: Suffix & ValidateLiteral<Suffix>,
): BrandFactory<`EndsWith${Suffix}`, string, EndsWithError<Suffix>>;

Defined in: packages/common/src/Type.ts:8007

String Brand requiring an exact, case-sensitive suffix.

Validation preserves the complete string, including the suffix. An empty suffix accepts every string allowed by the parent Type. The suffix must be one concrete string literal so different suffixes have distinct brands.

Like String.prototype.endsWith, matching compares UTF-16 code units, so a suffix starting with a low surrogate matches the second half of a character outside the Basic Multilingual Plane, such as an emoji.

Example

import {
  assertEqual,
  assertErr,
  assertOk,
  assertType,
  endsWith,
  maxLength,
  String,
  type Brand,
} from "@evolu/common";

const FileName = endsWith(".json")(maxLength(64)(String));

const name = FileName.fromUnknown("config.json");
assertOk(name, "config.json");
assertType<
  typeof name.value,
  string & Brand<"MaxLength64"> & Brand<"EndsWith.json">
>();

assertEqual(FileName.to(name.value), "config.json");

assertErr(FileName.fromUnknown("config.JSON"));
assertErr(FileName.fromUnknown("X".repeat(60) + ".json"));