Problem Statement
Generic constraint syntax:
Explanation
Use “extends” to restrict a generic. Read it aloud as: T must be a subtype of U. This lets you safely use U’s properties inside the generic body.
Code Solution
SolutionRead Only
function prop<T extends object, K extends keyof T>(obj: T, key: K) {
return obj[key];
}
// prop(123, 'x') // ❌ 123 is not an object