tezvyn:

Write a generic PickByValue<T, V> type

AI-drafted, machine-checkedSource: typescriptlang.orgadvanced

Tests conditional types and mapped-type key remapping. Great answer: K in keyof T as T[K] extends V ? K : never with value T[K]. Red flag: writing V extends T[K] instead, which reverses the assignability check and includes wrong keys.

WHAT THIS TESTS: This tests three core TypeScript type-system mechanics used together: mapped types, conditional types, and key remapping via the as clause. The interviewer wants to see if you can iterate over an object type's keys, evaluate a condition on each property's value type, and selectively keep or discard keys based on that condition. It also checks whether you understand the direction of assignability in conditional types.

A GOOD ANSWER COVERS: First, the candidate should write a mapped type over keyof T. Second, they must use the as keyword to remap keys: K in keyof T as T[K] extends V ? K : never. Third, the value side should simply be T[K]. Fourth, a strong candidate explains that T[K] extends V checks whether each property value is assignable to V, and that never as a key in a mapped type causes that property to be dropped entirely. Fifth, they might mention that prior to TypeScript 4.1 this required a more verbose helper approach using Pick and an intersection of keys, but modern TypeScript handles it inline with as.

COMMON WRONG ANSWERS: The most frequent error is reversing the conditional to V extends T[K]. This asks whether the filter type is assignable to the property, which is backwards; for example, it would keep boolean when filtering for string | number because string | number is not assignable to boolean, but the reverse is not what the question asks. Another red flag is omitting the as clause and writing K in keyof T: T[K] extends V ? T[K] : never, which produces an object where rejected keys still exist with value type never instead of being removed. Some candidates try to use the built-in Pick utility directly without a mapped type, but Pick requires a known key union and cannot perform conditional logic on value types.

LIKELY FOLLOW-UPS: The interviewer may ask how to handle optional properties and undefined, since T[K] for an optional key includes undefined and may affect the extends check. They might ask for a strict version that requires exact type equality rather than assignability, which would need an additional conditional or a helper like Equals X, Y. They may also ask for the inverse OmitByValue T, V, which simply flips the conditional to T[K] extends V ? never : K.

ONE CONCRETE EXAMPLE: For PickByValue with object name string, age number, active boolean and filter string | number, the type evaluates as follows. name has type string, and string extends string | number is true, so the key name is kept with value string. age has type number, and number extends string | number is true, so age is kept with value number. active has type boolean, and boolean extends string | number is false, so the key becomes never and is dropped. The final type is name string and age number.

Read the original → typescriptlang.org

Get five bites like this every day.

Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.