Design Patterns · Senior · Question 15 of 75
What is the expression problem, and how does it show up in TS?
- ✓Correct answer: Unions are easy to extend with operations, classes with new types
- 2TypeScript can't infer the types of complex expressions
- 3JSX expressions can't be typed without
as
Explanation
With a discriminated union, adding a new handler function is easy, but a new variant requires editing every switch. In OOP with classes, adding a new type is easy, but a new operation is expensive (solved with Visitor). The choice depends on what will change more often.