Type Alias AsValue<T>

AsValue: [T] extends [Value]
    ? T
    : T extends null
    | boolean
    | number
    | string
    | SerialisesItself
        ? T
        : T extends (...args: never[]) => unknown
            ? never
            : T extends readonly unknown[]
                ? { readonly [I in keyof T]: AsValue<T[I]> }
                : T extends object
                    ? {
                        readonly [K in keyof T]: K extends string
                        | number
                            ? AsValue<T[K]>
                            : never
                    }
                    : never

T itself when T is a value, and otherwise a type nothing is assignable to, for a function taking any value whose type it infers, interfaces included, as value: T & AsValue<T>.

Every part of T is checked: a function, undefined, a symbol, a bigint or an object with methods that does not serialise itself, at any depth, makes T not a value. Some types pass that are refused when they are given, as NaN, the infinities and cycles are:

  • an instance of a class whose members are all data, since types cannot see an object's prototype;
  • object and {}, which say nothing of what they hold, and are the type of an empty object too;
  • a property with a symbol key;
  • an optional property holding undefined, unless the compiler's exactOptionalPropertyTypes is on.
import assert from "node:assert/strict";
import type { AsValue } from "itinera/value";

interface Order {
id: string;
coupon?: string;
}

function keep<T>(value: T & AsValue<T>): T {
return value;
}

const order: Order = { id: "R-1" };

assert.equal(keep(order), order);
// @ts-expect-error: a function is not a value
keep({ id: "R-1", total: () => 1250 });

Type Parameters

  • T