Visit complete TypeScript roadmap
TypeScript Topic

keyof Operator

keyof Operator

The keyof operator in TypeScript is used to get the union of keys from an object type. Here’s an example of how it can be used:

interface User {
  name: string;
  age: number;
  location: string;
}

type UserKeys = keyof User; // "name" | "age" | "location"
const key: UserKeys = 'name';

In this example, UserKeys is a type that represents the union of keys from the User interface, which is "name" | "age" | "location". And a constant named key with the type UserKeys is declared with the value "name".

Learn more from the following links:

More Topics

Explore related content

View All Topics
Loved by 100K+ Developers

Start Your Learning
Journey Today

Join thousands of developers who are leveling up their skills with structured roadmaps and expert guidance

No credit card required
Always free
Track your progress