Skip to content
Justice Tech School

keyof

keyof gives the union of an object type's keys. It makes safe property access.

Example
const user = { id: 1, name: "Ada" };
function get<T, K extends keyof T>(o: T, k: K) { return o[k]; }
console.log(get(user, "name"));

Result

Ada

Exercise

Keys of a type.

 User

Quiz Test

What does keyof User give?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.