Skip to content
// EFFECT DAYS 2026

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.

18 exports Since v4.1.5 Source

Combining

Since v4.1.5

Joins code fragments with a source-text separator.

Signature

declare const join: (values: ReadonlyArray<CodeHole>, separator: string) => Fragment

Constructors

Since v4.1.5

Combines source text and bindings without resolving their names.

Signature

declare const code: (strings: TemplateStringsArray, ...values: ReadonlyArray<CodeHole>) => Fragment
Since v4.1.5

Defers source construction until its bindings can be rendered.

Signature

declare const deferred: (render: (render: Render) => string) => Fragment

Creates a default import binding that can be shared across generated modules.

Signature

declare function importDefault(module: string, local: string, options?: {
readonly typeOnly?: boolean;
}): Binding

Creates a named import binding that can be shared across generated modules.

Signature

declare function importNamed(module: string, name: string, options?: {
readonly typeOnly?: boolean;
}): Binding

Creates a namespace import binding that can be shared across generated modules.

Signature

declare function importNamespace(module: string, local: string, options?: {
readonly typeOnly?: boolean;
}): Binding

Formatting

Formats text as a JSDoc comment and escapes embedded comment terminators.

Signature

declare function docComment(text: string | readonly Array<string>): string
Since v4.1.5

Formats a primitive as a JavaScript literal, qualifying non-finite numbers with globalThis.

Signature

declare const literal: (value: string | number | bigint | boolean | null | undefined) => string

Formats an object property key, using a computed key for proto.

Signature

declare function propertyKey(name: string): string

Models

Binding

interface
Since v4.1.5

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;
}
Since v4.1.5

A fragment, primitive, or nested collection accepted by the code template helpers.

Signature

type CodeHole = Fragment | number | ReadonlyArray<CodeHole> | undefined | null | false
Since v4.1.5

A declaration binding with a name allocated in its owning module.

Signature

interface DeclarationBinding extends Binding {
readonly name: string;
}
Since v4.1.5

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;
}

Import

interface
Since v4.1.5

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;
}
Since v4.1.5

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;
}
Since v4.1.5

A function that resolves a fragment using the current module names and imports.

Signature

type Render = (fragment: Fragment) => string

Scope

interface
Since v4.1.5

A declaration scope belonging to one generated module.

Signature

interface Scope {
readonly _tag: "Scope";
}

Transforming

Since v4.1.5

Indents nonempty lines after the first line of a fragment by two spaces.

Signature

declare function indent(content: CodeHole): Fragment