API reference › @evolu/common › Type › typed
Call Signature
function typed<Tag>(
tag: ValidateTypedTag<Tag>,
): StrictObjectType<TypedProps<Tag, Readonly<Record<never, never>>>>;
Defined in: packages/common/src/Type.ts:8801
Tagged ObjectType.
The discriminator belongs to typed, so additional properties cannot declare
type. The discriminator Input is string, inherited from String,
while its Output is the exact tag. Without a third argument, additional
properties are rejected. Pass a record with the predefined String
key Type as the third argument to validate and preserve additional
string-keyed properties, just like object.
Example
import { String, discriminatedUnion, typed } from "@evolu/common";
const Pending = typed("Pending", {
label: String,
});
const Completed = typed("Completed");
const Status = discriminatedUnion(Pending, Completed);
expectOk(Status.fromUnknown({ type: "Pending", label: "Waiting" }), {
type: "Pending",
label: "Waiting",
});
expectOk(Status.fromUnknown({ type: "Completed" }), {
type: "Completed",
});
Call Signature
function typed<Tag, Props>(
tag: ValidateTypedTag<Tag>,
props: Props,
...validation: [TypedValidationError<Props>] extends [never]
? []
: [ValidationFailure<TypedValidationError<Props>>]
): StrictObjectType<TypedProps<Tag, Props>>;
Defined in: packages/common/src/Type.ts:8804
Tagged ObjectType.
The discriminator belongs to typed, so additional properties cannot declare
type. The discriminator Input is string, inherited from String,
while its Output is the exact tag. Without a third argument, additional
properties are rejected. Pass a record with the predefined String
key Type as the third argument to validate and preserve additional
string-keyed properties, just like object.
Example
import { String, discriminatedUnion, typed } from "@evolu/common";
const Pending = typed("Pending", {
label: String,
});
const Completed = typed("Completed");
const Status = discriminatedUnion(Pending, Completed);
expectOk(Status.fromUnknown({ type: "Pending", label: "Waiting" }), {
type: "Pending",
label: "Waiting",
});
expectOk(Status.fromUnknown({ type: "Completed" }), {
type: "Completed",
});
Call Signature
function typed<Tag, Props, Rest>(
tag: ValidateTypedTag<Tag>,
props: Props,
record: Rest,
...validation: [
| TypedValidationError<Props>
| ObjectRecordValidationError<TypedProps<Tag, Props>, Rest>,
] extends [never]
? []
: [
ValidationFailure<
| TypedValidationError<Props>
| ObjectRecordValidationError<TypedProps<Tag, Props>, Rest>
>,
]
): ObjectType<
TypedProps<Tag, Props>,
Rest extends ObjectRecordTypeNode ? Rest : never
>;
Defined in: packages/common/src/Type.ts:8814
Tagged ObjectType.
The discriminator belongs to typed, so additional properties cannot declare
type. The discriminator Input is string, inherited from String,
while its Output is the exact tag. Without a third argument, additional
properties are rejected. Pass a record with the predefined String
key Type as the third argument to validate and preserve additional
string-keyed properties, just like object.
Example
import { String, discriminatedUnion, typed } from "@evolu/common";
const Pending = typed("Pending", {
label: String,
});
const Completed = typed("Completed");
const Status = discriminatedUnion(Pending, Completed);
expectOk(Status.fromUnknown({ type: "Pending", label: "Waiting" }), {
type: "Pending",
label: "Waiting",
});
expectOk(Status.fromUnknown({ type: "Completed" }), {
type: "Completed",
});