structured types #

Types come two ways. The flat strings (typeSignature, type, returnType) are the TypeScript checker's own rendering. Beside them, a TypeJson tree in typeInfo (returnTypeInfo for return types) gives the structure, for rendering types with links and highlighting.

typeInfo appears on variables, type aliases, variable members, component props, parameters, and tuple elements; returnTypeInfo on functions, function members, overloads, and callback props.

When it's present
#

A tree is absent when the flat string says everything: intrinsics, bare references, and object and function types. It's present when it adds structure: union and intersection members, a reference's type arguments, array elements, and tuple elements with their labels and ?/... markers.

A type alias's typeSignature prints only its name, so an alias carries its tree whatever the shape, except object and function types, which its members cover. Intrinsic and literal aliases (type A = string) print structurally and get no tree.

Nodes
#

The node kinds are intrinsic, literal, reference, array, tuple, union, intersection, function, object, and other.

  • Union and intersection members keep alias names. Union members follow the order the author wrote them, with null and undefined last, while the flat strings use TypeScript's internal order, so read the tree for author intent. A union with no written form (inferred, keyof, mapped) keeps TypeScript's order in both.
  • A literal's value is its runtime value, except that a bigint is its text ("1n") and a non-finite number a string ("Infinity").
  • A named generic instantiation is a reference with typeArgs (Snippet<[a: string]>), and other callables are function nodes. Object literal and function types are object and function nodes carrying the checker's text.
  • Arrays and tuples carry readonly, and the optional-property undefined is dropped, since optional: true says it.

Lost alias names
#

TypeScript drops the name of an alias over an indexed access or conditional type, like type Foo = z.infer<typeof S> or valibot's InferOutput, and prints its expanded structure wherever it's used. The tree recovers the name as {kind: "reference", name: "Foo"}, from the annotation written at that position or by matching the project's exported aliases. A name recovered by matching also carries module, the ModuleJson.path declaring it.

An exported alias neither way recovers warns alias_lost (see diagnostics). Declaring it as an interface keeps its name: interface Foo extends z.infer<typeof S> {}, which documents the inferred properties as its members.

Rendering
#

typeJsonToTokens flattens a tree into tokens, deciding spacing, separators, and parentheses, so a renderer only decides what each token looks like: link name tokens, highlight code tokens, and print text as-is. typeJsonToText joins them into a plain string.

import {typeJsonToTokens} from 'svelte-docinfo/declaration-helpers.js'; if (declaration.typeInfo) typeJsonToTokens(declaration.typeInfo); // [{kind: 'name', name: 'Map'}, {kind: 'text', text: '<'}, // {kind: 'code', text: 'string'}, {kind: 'text', text: ', '}, // {kind: 'name', name: 'Tome'}, {kind: 'text', text: '>'}]

Module paths
#

TypeScript prints a module object as typeof import("…") with an absolute path. svelte-docinfo rewrites the path in every type text, flat and structured: a module in the output becomes its ModuleJson.path, usable as a lookup key, a package its path below node_modules/, and any other file a path relative to the project root. In a tree, a module object is an other node.