Sometimes, we want to get the keys of a TypeScript interface as array of strings.
In this article, we’ll look at how to get the keys of a TypeScript interface as array of strings.
How to get the keys of a TypeScript interface as array of strings?
To get the keys of a TypeScript interface as array of strings, 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 keys of a TypeScript interface as array of strings, we can use the keyof
keyword.