Interface SerialisesItself

An object that turns itself into a value, usually an instance of a class with a toValue() method.

itinera calls toValue() once, when the object is given, and keeps what it returns, so changing the object afterwards changes nothing that was given. A class whose instances a request reads back also has a static fromValue(value), which makes an instance again from what toValue() returned. A toValue() that throws, or returns what is not a value, makes the object not a value.

import assert from "node:assert/strict";
import type { SerialisesItself } from "itinera/value";

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

toValue(): { readonly cents: number } {
return { cents: this.cents };
}

static fromValue(value: { readonly cents: number }): Payment {
return new Payment(value.cents);
}
}

const payment = new Payment(1250);

assert.equal(Payment.fromValue(payment.toValue()).cents, 1250);
interface SerialisesItself {
    toValue(): Value;
}

Index