Sometimes, we want to get the union of the keys of a TypeScript interface.
In this article, we’ll look at how to get the union of the keys of a TypeScript interface.
How to get the keys of a TypeScript interface?
To get the union of the keys of a TypeScript interface, we can use the keyof keyword.
For instance, we write
interface Person {
name: string;
age: number;
location: string;
}
type Keys = keyof Person;
to create the Keys type by setting it to keyof Person.
keyof Person returns the literal type "name" | "age" | "location".
Conclusion
To get the union of the keys of a TypeScript interface, we can use the keyof keyword.