velt/reflect
API referenceWhat 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.
Exports
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).Re-exported from velt/reflect/reflect
function beginVisit(id: u64): boolStarts 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.
Re-exported from velt/reflect/reflect
function callToJSON(v: any, key: string): anyWhat 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): anyv 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(): voidEnds the innermost visit beginVisit started.
Re-exported from velt/reflect/reflect
function entryCount(v: any): i64How 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 aRecord<string, V>: its entry positions, in insertion order (a deleted entry is listed with the name""and the valueundefined);a
sharedcell: the value it holds;anything else: none.
Re-exported from velt/reflect/reflect
function entryName(v: any, i: i64): stringThe name of entry i of v (entryCount): a field's or a key's; "" for an element.
Re-exported from velt/reflect/reflect
function entryValue(v: any, i: i64): anyThe 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): u64The 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): boolWhether 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): stringThe kind of what v holds (typeOf(v).kind), without the rest.
Re-exported from velt/reflect/reflect
function typeOf(v: any): RuntimeTypeWhat v holds at run time.
Re-exported from velt/reflect/runtime_type
What a dynamic value holds at run time (typeOf).
Returned bytypeOf
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: stringi64, u8, f64, … for a number; "" otherwise.
readonly name: stringThe 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 yethasHook(name: string): boolWhether its class has the hook name (toJSON, toString, the inspect hook's name).