0 of 5 problems solved0%
4 of 5

Safe Get

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];
}

Your Task

Read a property with a key that matches the object type.

  • Implement function getValue<T, K extends keyof T>(obj: T, key: K): T[K].
  • Return obj[key] without extra copying.
  • Do not loosen the types (no any).