[API reference](https://evolu.dev/docs/api-reference) › [@evolu/common](https://evolu.dev/docs/api-reference/common) › [Type](https://evolu.dev/docs/api-reference/common/Type) › createTypeWithError

```ts
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](https://github.com/evoluhq/evolu/blob/dd96d79f1dbe9a49fa12ce8e0aa7d3d0177795ca/packages/common/src/Type.ts#L2280)

Creates a root [Type](https://evolu.dev/docs/api-reference/common/Type/interfaces/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

```ts
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.");
```