Discriminated unions use a literal property (like type) to label each variant. A switch or equality check on that field narrows to the right shape. This is handy when you want a common "view model" regardless of the original variant.
type U = { type: "user"; username: string } | { type: "org"; orgName: string };
// Check u.type and build a common { label } objectReturn a small object rather than a string to keep data flexible.
Produce a unified header object from different entity shapes.
type Entity = { type: 'user'; id: string; username: string } | { type: 'org'; id: string; orgName: string }.normalize(e: Entity): { id: string; label: string }.label = username; for an org, use label = orgName.