#2595Medium

PickByType

From `T`, pick a set of properties whose type are assignable to `U`. Master advanced TypeScript type manipulation in this medium-level challenge on TypeScriptPro.

In this medium-level challenge, you'll build a PickByType utility that selects only the properties from an object type T whose values are assignable to a given type U.

Challenge Instructions: PickByType

Medium

From T, pick a set of properties whose type are assignable to U.

For Example

type OnlyBoolean = PickByType<{
name: string
count: number
isReadonly: boolean
isEnable: boolean
}, boolean> // { isReadonly: boolean; isEnable: boolean; }

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

Loading...

Detailed Explanation

type PickByType<T, U> = {
  [K in keyof T as T[K] extends U ? K : never]: T[K]
}

How it works:

This is the inverse of OmitByType, which removes properties matching the given type. Together, these two utilities demonstrate how key remapping with as enables powerful property filtering patterns.

This challenge helps you understand key remapping in mapped types and how to apply conditional property selection based on value types in real-world scenarios.

This challenge is originally from here.

Share this challenge

Learn the Concepts

Join early, learn faster.

Be the first to access the course, unlock exclusive launch bonuses, and get special early-bird pricing before anyone else.

No spam, unsubscribe at any time. We respect your privacy.

Limited Availability

Only 27 Spots left

Early Access

Get 1 month early access

>75% Off

Pre-Launch discount