API reference › @evolu/common › Type › literal
function literal<Expected>(
expected: ValidateLiteral<Expected>,
): LiteralType<Expected>;
Defined in: packages/common/src/Type.ts:3392
Literal Type.
String, Number, BigInt, and Boolean literal Types
are children of their corresponding primitive Types and accept the widened
primitive through from.parent. The expected value must have one exact
literal type. Validation uses ===, so -0 matches 0.
In templateLiteralParser, use a string Literal Type when the literal should be decoded into the Output Tuple. Use a raw string when it should only frame the canonical string.
Example
import {
assertEqual,
assertErr,
assertOk,
assertType,
Data,
literal,
} from "@evolu/common";
const Ready = literal("ready");
assertType<"ready", typeof Ready.Output>();
assertOk(Ready.fromUnknown("ready"), "ready");
const invalid = Ready.fromUnknown("pending");
assertErr(invalid);
assertType(Data, invalid.error);
assertEqual(invalid.error, {
type: "Literal",
expected: "ready",
value: "pending",
});