Skip to content

unacy / Formatter

Type Alias: Formatter<TInput> ​

ts
type Formatter<TInput> = (input) => string;

Defined in: packages/core/src/formatters.ts:46

Formatter converts a format-tagged value to a string representation.

Type Parameters ​

Type ParameterDescription
TInput extends WithFormat<unknown, string>Format-tagged type to format (must extend WithFormat<T, F>)

Parameters ​

ParameterTypeDescription
inputTInputValue tagged with the format brand

Returns ​

string

Plain string representation (format brand is stripped in the output)

Remarks ​

  • Output string should be parseable by the corresponding Parser<TInput> to ensure round-trip integrity
  • The format brand carried by TInput is lost in the string output; callers must re-parse to recover a branded value

Example ​

typescript
type ISO8601Date = WithFormat<Date, 'ISO8601'>;
const formatISO: Formatter<ISO8601Date> = (date) => date.toISOString();

Use When ​

You need a typed function that can only receive format-validated values (via WithFormat<T, F>) before serialising them.

Avoid When ​

The value needs no format brand — accept a plain T and format without the tag overhead.

Pitfalls ​

NEVER call a Formatter on a plain (untagged) value without first wrapping it in the appropriate WithFormat<T, F> brand via a Parser — the brand exists to guarantee the value has been validated before formatting.

NEVER assume formatter output is stable across timezone environments — Date.toISOString() uses UTC but other formatters may be locale-sensitive.

See ​

  • Parser
  • FormatterParser

Released under the MIT License.