Codegen
Builds TypeScript modules with a mutable Module builder and structured code fragments.
Keep references as bindings while composing code. A module allocates declaration
names, resolves import aliases, orders eager dependencies, and prints the result.
Compose snippets with code and use join for lists with explicit separators.
Raw strings are emitted unchanged; the builder does not parse arbitrary source.
Combining
Constructors
Combines source text and bindings without resolving their names.
Signature
declare const code: (strings: TemplateStringsArray, ...values: ReadonlyArray<CodeHole>) => FragmentDefers source construction until its bindings can be rendered.
Signature
declare const deferred: (render: (render: Render) => string) => FragmentCreates a default import binding that can be shared across generated modules.
Signature
declare function importDefault(module: string, local: string, options?: { readonly typeOnly?: boolean;}): BindingCreates a named import binding that can be shared across generated modules.
Signature
declare function importNamed(module: string, name: string, options?: { readonly typeOnly?: boolean;}): BindingCreates a namespace import binding that can be shared across generated modules.
Signature
declare function importNamespace(module: string, local: string, options?: { readonly typeOnly?: boolean;}): BindingFormatting
Formats text as a JSDoc comment and escapes embedded comment terminators.
Signature
declare function docComment(text: string | readonly Array<string>): stringFormats a primitive as a JavaScript literal, qualifying non-finite numbers with globalThis.
Signature
declare const literal: (value: string | number | bigint | boolean | null | undefined) => stringFormats an object property key, using a computed key for proto.
Signature
declare function propertyKey(name: string): stringModels
A reference to a declaration or import whose identity is independent of its printed name.
Gotchas
Bindings throw on string coercion. Embed them with code and render the containing module.
Signature
interface Binding { readonly _tag: "Binding"; readonly preferredName: string; readonly toString: () => never;}A fragment, primitive, or nested collection accepted by the code template helpers.
Signature
type CodeHole = Fragment | number | ReadonlyArray<CodeHole> | undefined | null | falseDeclarationBinding
interfaceA declaration binding with a name allocated in its owning module.
Signature
interface DeclarationBinding extends Binding { readonly name: string;}Source text that retains declaration and import references until rendering.
Signature
type Fragment = string | Binding | { readonly _tag: "Parts"; readonly parts: ReadonlyArray<Fragment>; readonly toString: () => never;} | { readonly _tag: "Deferred"; readonly render: (render: Render) => string; readonly toString: () => never;}A structured import request with its preferred local name and optional import attributes.
Signature
interface Import { readonly attributes: string; readonly imported: string; readonly kind: "Namespace" | "Default" | "Named"; readonly local: string; readonly module: string; readonly typeOnly: boolean;}A mutable builder for a TypeScript module, with shared names, imports, and declaration ordering.
Details
Mutation methods return this builder for chaining. Allocation methods return bindings for use in code fragments. Printing produces a snapshot; the builder remains open for further declarations.
Signature
declare class Module { constructor(reserved: readonly Array<string>); add(fragment: Fragment): this; addImport(input: Import): Binding; addSchemas(schemas: Readonly<Record<string, SchemaRepresentation.Representation>>, options?: { readonly references?: References; readonly typeOnly?: boolean; }): ReadonlyMap<string, DeclarationBinding>; addSideEffect(module: string, attributes: string): this; declare(preferredName: string, options?: { readonly exact?: boolean; readonly scope?: Scope; }): DeclarationBinding; define(binding: Binding, fragments: readonly Array<Fragment>, dependencies: readonly Array<Binding>): this; finalize(roots: readonly Array<Fragment>): { readonly body: readonly Array<string>; readonly declarations: readonly Array<{ readonly binding: Binding; readonly fragments: readonly Array<string>; }>; readonly imports: readonly Array<string>; readonly names: ReadonlyMap<Binding, string>; readonly roots: readonly Array<string>; }; print(): string; provideImport(input: Import): Binding; scope(parent: Scope): Scope;}A function that resolves a fragment using the current module names and imports.
Signature
type Render = (fragment: Fragment) => stringA declaration scope belonging to one generated module.
Signature
interface Scope { readonly _tag: "Scope";}