Menu

Post image 1
Post image 2
Post image 3
1 / 3
0

The Strategy Pattern in TypeScript: Discriminated Unions Beat Subclasses

DEV Community·Gabriel Anhaia·30 days ago
#eMFsqDVV
Reading 0:00
15s threshold

Book: The TypeScript Type System — From Generics to DSL-Level Types Also by me: The TypeScript Library — the 5-book collection My project: Hermes IDE | GitHub — an IDE for developers who ship with Claude Code and other AI coding tools Me: xgabriel.com | GitHub You open a TypeScript file written by someone whose last language was Java. The payment module has the full kit: a PaymentStrategy interface, five concrete classes that implement charge() , a PaymentStrategyFactory that maps a string key to a class, and a registry the factory queries. The whole thing is around 200 lines. Adding a sixth provider means touching the interface, writing the class, registering it in the factory, and praying nobody forgot the case in the switch that lives inside the factory itself. The author is not a bad engineer. They are doing what the Gang of Four book told them to do in 1994. Strategy is one of the OG behavioural patterns: define a family of algorithms, encapsulate each, make them interchangeable.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.

Read More