API reference@evolu/commonType › ValidateLiteral

type ValidateLiteral<Expected> =
  IsUnion<Expected> extends false
    ? {} extends Readonly<Record<`${Expected}`, never>>
      ? LiteralCompileTimeError
      : Expected
    : LiteralCompileTimeError;

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

Requires one concrete literal type in a factory parameter.

Preserves an exact literal and produces a compile-time error for widened, union, branded, or open template literal types. Use an intersection with the inferred parameter type, as prefixed does. This guard performs no runtime validation.

Example

import { assertType, type ValidateLiteral } from "@evolu/common";

const definePrefix = <Prefix extends string>(
  prefix: Prefix & ValidateLiteral<Prefix>,
): Prefix => prefix;

const prefix = definePrefix("APP_");
assertType<typeof prefix, "APP_">();

const widened: string = "APP_";
// @ts-expect-error Expected must be one concrete literal value.
definePrefix(widened);