Type Alias Value

Value:
    | null
    | boolean
    | number
    | string
    | readonly Value[]
    | { readonly [key: string]: Value }
    | SerialisesItself

Data that can be serialised: plain data, as PlainValue, in which any part may be an object that SerialisesItself.

Functions, undefined, symbols, bigint, Date, Map and other objects with methods are not values, and do not compile where a Value is expected. An interface is not a Value either, since TypeScript gives an interface no index signature: AsValue checks one. A property with a symbol key compiles, since an index signature does not look at it, and is refused when it is given.

import assert from "node:assert/strict";
import type { PlainValue, Value } from "itinera/value";

class Payment {
constructor(readonly cents: number) {}

toValue(): PlainValue {
return { cents: this.cents };
}
}

function kind(value: Value): string {
return Array.isArray(value) ? "list" : typeof value;
}

assert.equal(kind(1250), "number");
assert.equal(kind(["tea", new Payment(1250)]), "list");
assert.equal(kind({ order: "R-1", payment: new Payment(1250) }), "object");
// @ts-expect-error: a Date is not a value
kind(new Date());