API reference@evolu/commonType › createTypeWithError

function createTypeWithError<Name, T, Error>(
  name: Name & ValidateConcreteTypeName<Name>,
  type: T & ValidateOutput<T> & IdentityEncodingOf<T> extends true
    ? unknown
    : "⛔ Type error: Source Type must use identity encoding.",
  mapError: (error: InferErrors<T>, value: unknown) => Error,
  formatError: TypeErrorFormatter<NoInfer>,
): Type<Name, T["Output"], T["Output"], Error>;

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

Creates a root Type with a custom error for an existing validator.

The source must use identity encoding. The new Type accepts its Output as Input and hides its parent boundaries. Validation and error collection are delegated automatically; the mapper receives the failure and original value. The formatter presents the mapped error as one issue.

Example

import {
  assertEqual,
  assertErr,
  createTypeWithError,
  Number,
  String,
  union,
  type TypeError,
  type UnionError,
} from "@evolu/common";

interface ValueError extends TypeError<"Value"> {
  readonly cause: UnionError;
}

const Value = createTypeWithError(
  "Value",
  union(String, Number),
  (cause): ValueError => ({ type: "Value", cause }),
  () => "Enter text or a number.",
);

const result = Value.fromUnknown(false, { errors: "all" });
assertErr(result);
assertEqual(result.error.cause.errors.length, 2);
assertEqual(Value.formatError(result.error), "Enter text or a number.");