import type { JSONValue } from "./json-value.js"; export interface DisplayStructOptions { readonly description: JSONValue; } export type Fields = Record; export function DisplayStruct( name: string, fields: Record, options?: DisplayStructOptions ): object { let displayName = name; if (options?.description) { displayName = `${displayName} [${ typeof options.description === "string" ? options.description : JSON.stringify(options.description) }]`; } const constructor = class {}; Object.defineProperty(constructor, "name", { value: displayName }); const object = new constructor(); for (const [key, value] of entries(fields)) { Object.defineProperty(object, key, { value, enumerable: true, }); } return object; } type Entries = { [P in keyof R]: [P, R[P]] }[keyof R]; function entries(object: R): Entries[] { return Object.entries(object) as Entries[]; }