tezvyn:

Write a generic CreateSetters<T> that maps properties to setter methods

AI-drafted, machine-checkedSource: typescriptlang.orgadvanced

This tests TypeScript mapped types with key remapping and template literals. It iterates keyof T, remaps via as set plus Capitalize of string and K, and types values as setter methods. Red flag: manually typing setters or omitting string and K in Capitalize.

WHAT THIS TESTS: This question evaluates whether you can manipulate types at the type level using TypeScript mapped types, key remapping with the as clause, and template literal types. It separates developers who only consume generics from those who can author complex type transformations. The interviewer wants to see comfort with keyof, indexed access types, and the Capitalize intrinsic string manipulation type.

A GOOD ANSWER COVERS: A good answer hits four things in order. First, declare a mapped type that iterates over each key in keyof T. Second, use the as keyword to remap each key into a new string literal type following the pattern set followed by Capitalize of string and K, where K represents the current property key. Third, assign the value type as a function accepting the original property type and returning void, written as a function with parameter value of type T indexed by K and returning void. Fourth, mention that string and K is necessary because Capitalize expects a string subtype, while keyof T could include symbol keys. An excellent candidate might also note how to handle optional or readonly modifiers if the interviewer asks.

COMMON WRONG ANSWERS: The most common mistake is omitting the as clause and trying to map over the original keys without renaming them. Another red flag is forgetting the string and K constraint inside Capitalize, which causes a type error because Capitalize does not accept generic PropertyKey types. Some candidates manually write out the resulting type instead of using a generic, which defeats the purpose. Others confuse the type system with runtime JavaScript and attempt to use Object.keys or string manipulation logic that belongs in value-level code.

LIKELY FOLLOW-UPS: Interviewers often ask how to make the setter return the object for chaining instead of void. Another follow-up is creating getters alongside setters in a single mapped type. They might ask how to filter out certain keys, for example excluding boolean properties from receiving setters, which would require combining mapped types with conditional types or the never type to suppress key generation.

ONE CONCRETE EXAMPLE: For a Config type with featureA as boolean and retries as number, the correct generic would look like type CreateSetters of T equals an object where K in keyof T as set concatenated with Capitalize of string and K maps to a function taking value of type T indexed by K and returning void. When applied to Config, this produces setFeatureA as a function from boolean to void and setRetries as a function from number to void. If you pass a symbol-keyed property, the string and K guard prevents an invalid template literal interpolation.

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.