Use two type parameters to relate an object T and one of its keys K. Constrain K with extends keyof T so the key is always valid. The return type is T[K], which matches the property’s type.
This pattern keeps getters type-safe and avoids stringly-typed access.
function pluck<T, K extends keyof T>(obj: T, key: K): T[K] {
return obj[key];
}
Read a property with a key that matches the object type.