Skip to content
velt/reflect exports

velt/reflect

API reference

What a dynamic value (any, unknown) holds at run time, read from its type descriptor: its kind and type, and the entries it lists (the fields of an object, the elements of an array, the entries of a Map). It is for libraries that walk values whose type they do not know: a serializer, a pretty-printer, a deep comparison. For a walkthrough, read the guide.

import { typeOf, entryCount, entryName, entryValue } from "velt/reflect";

Exports

beginVisitStarts visiting the value with identity id (identity): false when it is being visited already, so it contains itself.callToJSONWhat the toJSON(key) method of the class instance v holds returns (typeOf(v).hasHook( "toJSON")), undefined when it has none.dynamicOfv as an any, converted when it runs: what a walk does with a typed Proxy (isProxy) or an interface value.endVisitEnds the innermost visit beginVisit started.entryCountHow many entries v lists, or -1 when it cannot list them (a proxy, a Map with keys that are not strings, an object with a field no any can hold): - an object, a class instance or a struct: its fields, in JavaScript's key order (array index names first), without ES private fields (#x), symbol keys and functions; an object literal's getter is listed with the fields; - an array or a tuple: its elements; - a Map<string, V> or a Record<string, V>: its entry positions, in insertion order (a deleted entry is listed with the name "" and the value undefined); - a shared cell: the value it holds; - anything else: none.entryNameThe name of entry i of v (entryCount): a field's or a key's; "" for an element.entryValueThe value of entry i of v (entryCount): undefined for an absent optional field, a deleted entry or an i out of range.identityThe identity of v: the same number for the same object, array or function (an any holding one included), 0 for a value without one (a number, a string, a struct held by value).isProxyWhether v, of a class or object type, is a typed Proxy (always false for other types).kindOfThe kind of what v holds (typeOf(v).kind), without the rest.typeOfWhat v holds at run time.RuntimeTypeWhat a dynamic value holds at run time (typeOf).
function

beginVisit

#std/reflect/reflect.vlt:98

Re-exported from velt/reflect/reflect

function beginVisit(id: u64): bool

Starts visiting the value with identity id (identity): false when it is being visited already, so it contains itself. Visits nest per thread; each successful beginVisit needs an endVisit.

function

callToJSON

#std/reflect/reflect.vlt:68

Re-exported from velt/reflect/reflect

function callToJSON(v: any, key: string): any

What the toJSON(key) method of the class instance v holds returns (typeOf(v).hasHook( "toJSON")), undefined when it has none. For a proxy (kind "proxy"), the plain value Node's JSON.stringify reads through its traps: an array of its elements for an array target, else an object of its own enumerable keys and their values.

Re-exported from velt/reflect/reflect

function dynamicOf<T>(v: T): any

v as an any, converted when it runs: what a walk does with a typed Proxy (isProxy) or an interface value. Unlike v as any, it asks nothing more of T's values than reading them.

Re-exported from velt/reflect/reflect

function endVisit(): void

Ends the innermost visit beginVisit started.

function

entryCount

#std/reflect/reflect.vlt:45

Re-exported from velt/reflect/reflect

function entryCount(v: any): i64

How many entries v lists, or -1 when it cannot list them (a proxy, a Map with keys that are not strings, an object with a field no any can hold):

  • an object, a class instance or a struct: its fields, in JavaScript's key order (array index names first), without ES private fields (#x), symbol keys and functions; an object literal's getter is listed with the fields;

  • an array or a tuple: its elements;

  • a Map<string, V> or a Record<string, V>: its entry positions, in insertion order (a deleted entry is listed with the name "" and the value undefined);

  • a shared cell: the value it holds;

  • anything else: none.

Re-exported from velt/reflect/reflect

function entryName(v: any, i: i64): string

The name of entry i of v (entryCount): a field's or a key's; "" for an element.

function

entryValue

#std/reflect/reflect.vlt:58

Re-exported from velt/reflect/reflect

function entryValue(v: any, i: i64): any

The value of entry i of v (entryCount): undefined for an absent optional field, a deleted entry or an i out of range. Reading a getter's entry runs the getter.

Re-exported from velt/reflect/reflect

function identity<T>(v: T): u64

The identity of v: the same number for the same object, array or function (an any holding one included), 0 for a value without one (a number, a string, a struct held by value).

Re-exported from velt/reflect/reflect

function isProxy<T>(v: T): bool

Whether v, of a class or object type, is a typed Proxy (always false for other types).

Re-exported from velt/reflect/reflect

function kindOf(v: any): string

The kind of what v holds (typeOf(v).kind), without the rest.

Re-exported from velt/reflect/reflect

function typeOf(v: any): RuntimeType

What v holds at run time.

Re-exported from velt/reflect/runtime_type

What a dynamic value holds at run time (typeOf).

readonly kind: string

"undefined", "null", "boolean", "number", "string", "symbol", "function", "array", "tuple", "object" (an object type), "class", "struct", "map", "record", "set", "shared", "proxy", "interface", "enum" or "opaque" (a value whose structure is not shown, such as a promise).

readonly repr: string

i64, u8, f64, … for a number; "" otherwise.

readonly name: string

The type's name as messages show it (User, Map, number).

readonly hooks: string[]

The hooks its class has, of toJSON, toString and the inspect hook (a method keyed by Symbol.for("nodejs.util.inspect.custom"), named as velt_sema's hooks::INSPECT names it).

constructor(kind: string, repr: string, name: string, hooks: string[])No doc comment yet
hasHook(name: string): bool

Whether its class has the hook name (toJSON, toString, the inspect hook's name).