Complete worked example
typescript
type Model = { title: string; count: number };
type Change = { key: "title"; value: string } | { key: "count"; value: number };
function apply(model: Model, change: Change): Model {
return change.key === "title" ? { ...model, title: change.value } : { ...model, count: change.value };
}- Reasoning
- Branching on the tag preserves the payload type needed by the corresponding property.