Which of these getProperty calls compile, and what does each return?
K extends keyof T limits the key to the keys of the object you pass. Name the type each ^? line points at, and every error TypeScript reports.
function getProperty<T, K extends keyof T>(obj: T, key: K): T[K] { return obj[key];} const user = { id: 1, name: "Ada", admin: false }; const name = getProperty(user, "name");// ^?const field = Math.random() > 0.5 ? "id" : "name";const value = getProperty(user, field);// ^?getProperty(user, "email");let key = "id";getProperty(user, key);Work it out first: the compiler shows every error and type.
The answer
Work out every error TypeScript reports and every type it shows, line by line. Then check.
Checking your sign-in…