HardUnion & Intersection💻 Output Question

Union narrowing exhaustion — never in the default case

💡

Hint

Assigning the default case to a variable of type never is TypeScript's exhaustiveness check pattern. If the union gains a new member that isn't handled, TypeScript errors because that member is not assignable to never.

What does this output?

// Simulate exhaustive union checking with never
// type Status = 'active' | 'inactive' | 'pending'

function getStatusLabel(status) {
  switch (status) {
    case 'active':   return 'Active User';
    case 'inactive': return 'Inactive User';
    case 'pending':  return 'Pending Approval';
    default:
      // In TypeScript: const _check: never = status
      // If all cases covered, default is unreachable — _check is never
      // If a new case added without handling, _check errors at compile time
      return 'Unknown: ' + status;
  }
}

const statuses = ['active', 'inactive', 'pending', 'banned'];
statuses.forEach(s => console.log(getStatusLabel(s)));

// At compile time, if Status = 'active' | 'inactive' | 'pending':
// The default case has status: never — TypeScript can prove it's unreachable
console.log(statuses.length);

Correct Output

Active User
Inactive User
Pending Approval
Unknown: banned
4

Why this output?

Explanation: All three defined cases are handled. "banned" falls through to default — at runtime it returns Unknown: banned. TypeScript's never type would catch this at compile time if "banned" were added to the type without a case.

Key Insight: Assigning the default case to a variable of type never is TypeScript's exhaustiveness check pattern. If the union gains a new member that isn't handled, TypeScript errors because that member is not assignable to never.

More Union & Intersection Output Questions

MediumUnion type — in operator narrows to the correct variantMediumIntersection type — must satisfy all merged properties

Practice predicting output live →

66 output questions with instant feedback

💻 Try Output Quiz