function typeOfValue
typeOfValue(
value: unknown,
depth?: number
): string

A TypeScript type for a runtime value — typeof, for people who want an answer.

typeOfValue rather than typeOf because it is the only one of the three layers that takes a VALUE: session.typeOf(expression) asks the page, the page runs this, and describeType(session, …) above both prefers a written type to either. Naming them all typeOf would make the call chain unreadable in a stack trace.

Structural, because that is what is knowable here: the page holds values, not declarations, and const a: string = 'x' left no trace of the annotation by the time it is a string. What a WRITTEN type looks like is a question for the file it was written in — the caller asks that first and only falls back to this.

Names classes and host objects rather than expanding them: HTMLDivElement says more than the two hundred properties it has, and the same is true of anything with a constructor of its own.

import { typeOfValue } from './type-of-value.ts';

typeOfValue('hi'); // 'string'
typeOfValue([1, 2]); // 'number[]'
typeOfValue({ a: 1, b: 'x' }); // '{ a: number; b: string }'
typeOfValue([1, 'x']); // '(number | string)[]'
typeOfValue(new Map([['k', 1]])); // 'Map<string, number>'
typeOfValue(null); // 'null'

Parameters

value: unknown
optional
depth: number = 2

Return Type

string

Usage

import { typeOfValue } from "lib/repl/type-of-value.ts";