0 of 6 problems solved0%
3 of 6

Normalize Entities

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 } object

Return a small object rather than a string to keep data flexible.

Your Task

Produce a unified header object from different entity shapes.

  • Define type Entity = { type: 'user'; id: string; username: string } | { type: 'org'; id: string; orgName: string }.
  • Implement function normalize(e: Entity): { id: string; label: string }.
  • For a user, use label = username; for an org, use label = orgName.
  • Do not mutate the input.