API reference › @evolu/common › Type › typed
Call Signature
function typed<Tag>(tag: Tag): TypedType<Tag>;
Defined in: packages/common/src/Type.ts:3528
Creates a runtime-validated typed object with a type discriminant.
Example
const Card = typed("Card", {
cardNumber: CardNumber,
expiry: DateIso,
});
const Cash = typed("Cash", {
currency: NonEmptyTrimmedString,
});
const Payment = union(Card, Cash);
type Payment = typeof Payment.Type;
const result = Payment.fromUnknown(data);
if (result.ok) {
switch (result.value.type) {
case "Card":
console.log(result.value.cardNumber);
break;
case "Cash":
console.log(result.value.currency);
break;
}
}
See
Typed for type-only discrimination.
Call Signature
function typed<Tag, Props>(tag: Tag, props: Props): TypedType<Tag, Props>;
Defined in: packages/common/src/Type.ts:3530
With additional properties.