tezvyn:

Write a generic type-safe getProperty using keyof

AI-drafted, machine-checkedSource: typescriptlang.orgintermediate

Command of TypeScript generics and keyof for compile-time property access. Declare generic T, accept key as keyof T, and return T[K].

RED FLAG

Using any or string for the key, which erases type safety and allows invalid properties.

WHAT THIS TESTS: This question evaluates whether you understand TypeScript's type system beyond basic annotations. Specifically, it tests if you can combine generics with the keyof operator to create a reusable utility that preserves type information. The interviewer wants to see that you think in terms of type relationships rather than runtime behavior, and that you understand how to prevent invalid keys at compile time.

A GOOD ANSWER COVERS: First, declare the function with two type parameters: T for the object and K for the key. Constrain K to extends keyof T so TypeScript rejects keys that do not exist on T. Second, set the return type to T[K], which tells the compiler to look up the property type on the object type. Third, mention that T should be constrained to an object type, such as T extends object or T extends Record<string, any>, to prevent primitives from being passed. Fourth, note that this pattern is the foundation for built-in utilities like Pick and Partial, showing you understand the broader type manipulation ecosystem.

COMMON WRONG ANSWERS: A red flag is typing the key parameter as string instead of K extends keyof T, because string allows any property name and destroys compile-time safety. Another mistake is using any for the return type, which defeats the purpose of the generic. Some candidates forget to constrain T to an object, which can lead to confusing errors when primitives are passed. Also, returning T[keyof T] instead of T[K] is incorrect because keyof T is a union of all keys, so T[keyof T] becomes a union of all property types rather than the specific type for the given key.

LIKELY FOLLOW-UPS: The interviewer may ask how to make this work with optional properties and undefined return types. They might also ask how to restrict K to only string keys while excluding symbols and numbers, or how to implement a setProperty counterpart that enforces the correct value type. Another follow-up is handling path-based access like getProperty(obj, a.b.c) using template literal types and recursive type lookups.

ONE CONCRETE EXAMPLE: Consider an interface User with name: string and age: number. With the generic function getProperty with type parameters T and K extends keyof T, taking obj of type T and key of type K, and returning T[K], calling getProperty(user, name) returns string, and calling getProperty(user, email) produces a compile-time error because email is not a key of User. This gives immediate feedback in the IDE without running the code.

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.