Skip to content
Kitbay
Skill

ast-visitor-pattern

Apache-2.0Updated on Nov 11, 202647,664GitHub stars

A skill from the Prisma ORM repository's contributed skills describing when and how to model TypeScript discriminated unions as frozen classes with a visitor, instead of switch statements.

What the skill teaches

A skill from the Prisma ORM repository's contributed skills describing when and how to model TypeScript discriminated unions as frozen classes with a visitor, instead of switch statements. It applies when a set of variants, such as commands or tree nodes, is dispatched on in two or more places, and only for three or more variants; smaller cases keep a plain union and switch. The agent writes a hidden abstract base class with a kind field, an accept method and a freeze helper, a visitor interface with one method per variant, readonly subclasses and an exported union, and always builds instances with new, since frozen object literals break instanceof and accept.

Capabilities

  • Decide when a visitor beats a switch statement
  • Write frozen variant classes with an accept method
  • Define a visitor interface per union