ast-visitor-pattern
What it does
It guides your agent to structure a set of related types with frozen classes and a visitor, so every place that handles them is checked. It is for developers working in TypeScript with growing switch statements.
When to use it
- βWhen a set of variants is handled in two or more places, such as renderers or serializers.
- βWhen you are adding a new variant to a type that is already switched over in several places.
- βWhen a union type has grown and its switch statements are hard to keep consistent.
Skill link
https://github.com/prisma/orm/blob/main/skills-contrib/ast-visitor-pattern/SKILL.mdCopy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.
How to use
The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.
Ready-to-paste message
For Claude Code, Codex or Cursor: run it in your project folder and the skill gets installed.
Download the SKILL.md and drop it into your agentβs skills folder.
Example of what to ask your AI
βCan you refactor this union type and its switch statements so the compiler catches any missing case?β
5 new skills a week. 1 minute to read.
How it works
The agent turns each variant into a frozen class with an accept method, and defines a visitor interface that every consumer must implement. Adding a variant then causes a compile error wherever the visitor is not updated.
Technical content (original SKILL.md)
Original text by the author, straight from GitHub.
AST Class/Visitor Pattern
When a discriminated union has 3+ variants and 2+ dispatch sites (renderers, serializers, classifiers, etc.), replace plain union + switch with frozen subclasses and a visitor interface. This makes adding a new variant a compiler error at every consumer, instead of a silent omission.
Structure
Four pieces, usually in one file β the Mongo DDL set below spreads them over three:
// 1. Abstract base (not exported β consumers use the union type)
abstract class FooNode {
abstract readonly kind: string;
abstract accept<R>(visitor: FooVisitor<R>): R;
protected freeze(): void { Object.freeze(this); }
}
// 2. Visitor interface
export interface FooVisitor<R> {
bar(node: BarNode): R;
baz(node: BazNode): R;
}
// 3. Concrete subclasses β readonly fields, freeze() in constructor
export class BarNode extends FooNode {
readonly kind = 'bar' as const;
readonly value: string;
constructor(value: string) {
super();
this.value = value;
this.freeze();
}
accept<R>(visitor: FooVisitor<R>): R { return visitor.bar(this); }
}
export class BazNode extends FooNode {
readonly kind = 'baz' as const;
readonly count: number;
constructor(count: number) {
super();
this.count = count;
this.freeze();
}
accept<R>(visitor: FooVisitor<R>): R { return visitor.baz(this); }
}
// 4. Union type
export type Foo = BarNode | BazNode;
Consuming
Define a visitor object (or class) per concern:
const renderVisitor: FooVisitor<string> = {
bar(node) { return node.value; },
baz(node) { return String(node.count); },
};
function render(node: Foo): string {
return node.accept(renderVisitor);
}
Always construct instances, never frozen object literals
This holds everywhere a node is built β tests and production construction surfaces (contract-free factories, builders). A factory must return new BarNode(...), never Object.freeze({ kind: 'bar', value: 'x' }). A frozen plain object has no prototype, so instanceof fails, accept() is missing, and a downstream shallow-copy ({ ...node }) silently strips the type back to an anonymous bag; constructor-time invariants are skipped too.
// β
const call = new BarNode('x');
export function bar(value: string): BarNode { return new BarNode(value); }
// β
const call: Foo = { kind: 'bar', value: 'x' };
export function bar(value: string): Foo { return Object.freeze({ kind: 'bar', value }); }
When NOT to use
- Single dispatch site β plain union + switch is simpler
- Fewer than 3 variants with no expected growth β not worth the boilerplate
Codebase examples
MongoAstNodebase βpackages/2-mongo-family/4-query/query-ast/src/ast-node.tsMongoDdlCommandVisitorinterface βpackages/2-mongo-family/4-query/query-ast/src/ddl-visitors.ts- Concrete DDL commands and their union β
packages/2-mongo-family/4-query/query-ast/src/ddl-commands.ts
FAQ
When is this pattern worth it?
The skill suggests it when a union has three or more variants and two or more places that switch on it.
What language is the example in?
The example is in TypeScript, so the pattern is written for TypeScript code.
What does the compiler check?
Adding a new variant causes an error at every consumer that does not handle it, instead of being silently missed.
Details
Original description by the author
Use the frozen-class/visitor pattern for discriminated unions that have multiple dispatch sites. Use when creating a new set of variants (commands, IR nodes, factory calls) that will be switched over in 2+ places, or when refactoring an existing union type that has grown multiple switch sites.
repo: prisma/orm
Popularity
- GitHub stars
- 48k
- Ranking position
- #6309
- License
- Apache-2.0
- Updated
- 11/2/2026
Comments
- β¦
More skills from this repository
adr-review
48kTeaches your agent to review architecture decision records with fresh eyes, then rewrite them so the decision is clear to new readers.
markdown-no-artificial-line-wraps
48kHelps your agent write Markdown with whole paragraphs on one line, so text wraps neatly on screen and file changes stay clean.
multiline-commit-messages
48kShows your agent a safe way to write multi-line git commit messages in a shell, so the message does not come out garbled.
product-direction
48kHelps your agent judge feature requests against a project's written product direction, give a reasoned verdict, and record each no.
Similar skills
diffs
394kLets your agent produce a real, shareable diff of edits as a viewer link or image file, instead of a hand-written summary.
coding-agent
394kHelps your agent hand a larger coding job to Codex, Claude Code, or OpenCode as a background worker, then check on its progress.
gh-issues
394kHelps your agent find GitHub issues, pick the ones to work on, and open pull requests with fixes, including follow-up on review comments.