API reference@evolu/commonType › typed

Call Signature

function typed<Tag>(
  tag: ValidateTypedTag<Tag>,
): StrictObjectType<TypedProps<Tag, Readonly<Record<never, never>>>>;

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

Creates an ObjectType with a literal type property.

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 {
  assertFalse,
  assertOk,
  assertType,
  String,
  discriminatedUnion,
  typed,
} from "@evolu/common";

const Loading = typed("Loading");
const Loaded = typed("Loaded", { value: String });
const State = discriminatedUnion(Loading, Loaded);

assertOk(State.fromUnknown({ type: "Loading" }), { type: "Loading" });
assertOk(State.fromUnknown({ type: "Loaded", value: "Evolu" }), {
  type: "Loaded",
  value: "Evolu",
});
assertFalse(Loading.is({ type: "Loading", progress: 1 }));
assertType<
  true,
  typeof Loading.Output extends { readonly type: "Loading" } ? true : false
>();

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:11860

Creates a Tagged Object Type with declared properties.

Example

import { assertOk, String, typed } from "@evolu/common";

const Pending = typed("Pending", {
  label: String,
});

assertOk(Pending.fromUnknown({ type: "Pending", label: "Waiting" }), {
  type: "Pending",
  label: "Waiting",
});

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:11900

Creates a Tagged Object Type with additional record properties.

Example

import { assertOk, assertType, String, record, typed } from "@evolu/common";

const Open = typed("Open", { label: String }, record(String, String));
const result = Open.fromUnknown({
  type: "Open",
  label: "Ready",
  note: "Connected",
});

assertOk(result, {
  type: "Open",
  label: "Ready",
  note: "Connected",
});
assertType<string | undefined, typeof result.value.note>();