API reference@evolu/commonType › prefixed

function prefixed<Prefix>(
  prefix: Prefix & ValidateLiteral<Prefix>,
): <T>(
  type: T & ValidateOutput<T> & string extends T["Input"]
    ? unknown
    : "⛔ Type error: Prefixed Type Input must accept every string.",
) => PrefixedType<Prefix, T>;

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

Decodes a prefixed string with another Type and restores the prefix when encoding.

Uses startsWith to validate an exact, case-sensitive prefix before removing one occurrence. The wrapped Type validates and decodes the suffix; its constraints apply to the suffix, and its Output is preserved. Encoding prepends the prefix to the wrapped Type's canonical string representation. An empty prefix leaves that representation unchanged.

The prefix must be one concrete string literal. The wrapped Type must accept a string Input and encode to strings; its Output can have another type, as with PortFromString.

Example

import {
  assertEqual,
  assertErr,
  assertOk,
  assertType,
  ConstantCaseIdentifier,
  prefixed,
  PortFromString,
  type Port,
} from "@evolu/common";

const EnvName = prefixed("APP_")(ConstantCaseIdentifier);

const name = EnvName.fromUnknown("APP_PORT");
assertOk(name, "PORT");
assertType<typeof name.value, ConstantCaseIdentifier>();

assertEqual(EnvName.to(name.value), "APP_PORT");

assertErr(EnvName.fromUnknown("OTHER_PORT"));
assertErr(EnvName.fromUnknown("APP_port"));

const PortSetting = prefixed("port:")(PortFromString);

const port = PortSetting.fromUnknown("port:04000");
assertOk(port, 4000);
assertType<typeof port.value, Port>();

assertEqual(PortSetting.to(port.value), "port:4000");