#33763β€’Hard

Union to Object from key

Filter a union of object types down to the members that have a given key. No recursion, no infer: the whole difficulty is understanding distribution.

UnionToObjectFromKey<Union, Key> takes a union of object types and a key name, and returns the member(s) that actually have that key. The solution is compact, but it hinges on two mechanics you'll use constantly in advanced TypeScript: distributive conditional types and per-member keyof checks. The same distribute-and-filter idea underlies how TypeScript narrows discriminated unions.

For example

type Foo = { foo: string; common: boolean }
type Bar = { bar: number; common: boolean }
 
type Result = UnionToObjectFromKey<Foo | Bar, 'foo'> // expected to be Foo

Challenge Instructions: Union to Object from key

Hard

Find the object containing the key in the union type by the key. It takes two parameters: a union of object types and a key name.

View on GitHub: https://tsch.js.org/33763

Change the following code to make the test cases pass (no type check errors).

πŸ‘‹ Lifetime-License is leaving on August 10, 2026

Get it now for $29

One-time payment. Lifetime access to all pro challenges.

Loading...

Detailed Explanation

Everything fits in two nested conditionals:

type UnionToObjectFromKey<Union, Key> = Union extends unknown
  ? Key extends keyof Union
    ? Union
    : never
  : never

Each conditional has a distinct job.

Step 1: Distribute over the union

The outer check Union extends unknown is always true, since every type extends unknown, so it never filters anything. Its only purpose is to trigger distribution: when the type before extends is a bare generic parameter and it's instantiated with a union, TypeScript evaluates the conditional once per member and unions the results.

So for Union = Foo | Bar, the type expands to:

// (Key extends keyof Foo ? Foo : never)
//   | (Key extends keyof Bar ? Bar : never)

Without this wrapper, keyof Union inside would mean keys shared by all members (keyof (Foo | Bar) is only 'common'), and you could never match a key that exists on just one member. Distribution gives you each member's own keyof instead.

Step 2: Test the key against each member

Inside the distributed branch, Union refers to a single member at a time. Key extends keyof Union asks: is Key one of this member's keys?

This "return the member or never, let never disappear" idiom is how the built-in Extract and Exclude utilities work under the hood. You've effectively written a custom Extract that filters by key presence instead of assignability.

Walking the test cases

Why this counts as hard

There's no recursion and no infer here; the difficulty is conceptual. You need to know that a no-op conditional like Union extends unknown exists purely to switch on distribution, and that keyof behaves very differently on a whole union than on its members. Add the fact that never drops out of unions, and the solution almost writes itself. You'll spot the pattern in plenty of real-world types that pick a variant out of a discriminated union.

This challenge is originally from here.

Share this challenge

Learn the Concepts