Zum Inhalt springen
Kitbay
Skill

ast-visitor-pattern

Apache-2.0Aktualisiert am 11. Nov. 202647.664GitHub-Sterne

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.

Was der Skill vermittelt

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.

Funktionen

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