API Reference / @evolu/common / Type / base

Function: base()

function base<Name, T, Error>(name, fromUnknown): Type<Name, T, T, Error>;

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

Base Type.

A Base Type validates that a value conforms to a specific TypeScript type. Unlike refinements or transformations, Base Types establish the fundamental shape of a value before any branding or transformation occurs.

  • To refine a Base Type further, use the brand Type Factory.
  • To transform a Base Type into a different representation, use the transform Type Factory.

Example

const String = base("String", (value) =>
  typeof value === "string"
    ? ok(value)
    : err<StringError>({ type: "String", value }),
);

interface StringError extends TypeError<"String"> {}

const formatStringError = createTypeErrorFormatter<StringError>(
  (error) => `A value ${error.value} is not a string`,
);

Type Parameters

Type Parameter
Name extends string
T
Error extends TypeError<Capitalize<string>>

Parameters

ParameterType
nameName
fromUnknown(value) => Result<T, Error>

Returns

Type<Name, T, T, Error>

Was this page helpful?