Codecs
Pair a morph with its inverse to encode output back into input.
A Type with morphs decodes input into output. A codec is a morph with an inverse, so its output can also be encoded back into input.
Defining a codec
codec takes an input definition, an output definition and a function for each direction. It comes from arktype/codecs, which installs encoding when imported, so an app that doesn't encode doesn't ship it. Without that import, reading .encode throws a ParseError:
import { codec } from "arktype/codecs"
const const EpochMs: Type<(In: number) => To<Date>>EpochMs = codec("number.integer", "Date", {
decode: n => new Date(n),
encode: d => d.getTime()
})
// Date
const const date: Date | ArkErrorsdate = EpochMs(0)
// 0
const const ms: number | ArkErrorsms = const EpochMs: Type<(In: number) => To<Date>>EpochMs.encode(const date: Date | ArkErrorsdate)A codec is a Type. .encode is a Type too, from output to input, so it validates what it receives like any other Type:
// ArkErrors: must be a Date
const const out: number | ArkErrorsout = const EpochMs: Type<(In: number) => To<Date>>EpochMs.encode("0")What encode returns is validated against the input definition, so encoding always produces something the codec can decode.
Composition
Every Type has .encode, which inverts each of its morphs, so a codec encodes wherever it is nested: in objects, arrays and tuples, unions, intersections, pipes and cyclic scopes.
const const Event: Type<{
name: string;
at: (In: number) => To<Date>;
reminders?: ((In: number) => To<Date>)[];
}>
Event = type({
name: "string",
at: const EpochMs: Type<(In: number) => To<Date>>EpochMs,
"reminders?": const EpochMs: Type<(In: number) => To<Date>>EpochMs.array()
})
// { name: "launch", at: 0, reminders: [60000] }
const const encoded: ArkErrors | {
name: string;
at: number;
reminders?: number[];
}
encoded = const Event: Type<{
name: string;
at: (In: number) => To<Date>;
reminders?: ((In: number) => To<Date>)[];
}>
Event.encode({
name: "launch",
at: new Date(0),
reminders: [new Date(60000)]
})A Type without morphs encodes as itself.
Defaults and unions
A defaulted key is always present in decoded output, so it is required when encoding:
const const Settings: Type<{
since: (In: Default<number, 0>) => To<Date>;
}>
Settings = type({ since: const EpochMs: Type<(In: number) => To<Date>>EpochMs.default(0) })
// ArkErrors: since must be a Date (was missing)
const const out: ArkErrors | {
since: number;
}
out = const Settings: Type<{
since: (In: Default<number, 0>) => To<Date>;
}>
Settings.encode({})A union encodes a value with a branch that accepts it, trying any branch without morphs first so a value it decodes unchanged is encoded unchanged. If two branches with morphs accept overlapping values when encoding, accessing .encode throws a ParseError—the same indeterminate-union error ArkType throws when decoding overlapping morphs.
Keywords
The parse keywords encode back to the string they parse: string.date.parse, string.date.iso.parse and string.date.epoch.parse, string.numeric.parse, string.integer.parse, string.boolean.parse, string.json.parse and string.url.parse.
import "arktype/codecs"
const const Event: Type<{
at: (In: string) => To<Date>;
count: (In: string) => To<number>;
}>
Event = type({
at: "string.date.iso.parse",
count: "string.integer.parse"
})
// { at: "1970-01-01T00:00:00.000Z", count: "5" }
const const encoded: ArkErrors | {
at: string;
count: string;
}
encoded = const Event: Type<{
at: (In: string) => To<Date>;
count: (In: string) => To<number>;
}>
Event.encode({ at: new Date(0), count: 5 })string.trim, string.lower, string.upper, string.capitalize and string.normalize encode a string already in their format as itself, and object.json.stringify encodes by parsing.
Precompiling
Precompiled Types encode too. The build precompiles each codec like any other Type, passing its decode and encode functions where you wrote them, and each .encode it sees read becomes an encoder of its own:
import { codec } from "arktype/codecs"
const const EpochMs: Type<(In: number) => To<Date>>EpochMs = codec("number.integer", "Date", {
decode: n => new Date(n),
encode: d => d.getTime()
})
export const const Event: Type<{
at: (In: number) => To<Date>;
}>
Event = type({ at: const EpochMs: Type<(In: number) => To<Date>>EpochMs })
// precompiled to an encoder
export const encode = (event: unknown) => const Event: Type<{
at: (In: number) => To<Date>;
}>
Event.encode(event)A standalone build fails where .encode is read in a module that doesn't import arktype/codecs, itself or through a module it imports, or where the Type has a morph without an inverse, with the ParseError arktype would throw there. A build may see only the module it's reading, so importing arktype/codecs elsewhere in the program, like in its entry, isn't enough.
Morphs without an inverse
Encoding is only possible if every morph has an inverse. Otherwise, accessing .encode throws a ParseError:
import "arktype/codecs"
const const Length: Type<{
length: (In: string) => Out<number>;
}>
Length = type({ length: type("string").pipe(s => s.length) })
// ParseError: A morph must have an inverse to be encoded: (In: string) => Out<unknown>
const Length: Type<{
length: (In: string) => Out<number>;
}>
Length.encode