
上QQ阅读APP看书,第一时间看更新
The keyof operator
The keyof operator can be used to generate a union type of the properties of an object as string literal types:
interface User {
name: string;
age: number;
}
type userKeys = keyof User; // "name" | "age"
The keyof operator can be used in combination with other operators, such as the typeof operator, for example:
let person = { name: "Remo", age: "28" };
interface User {
name: string;
age: number;
}
type userKeys = keyof typeof person; // "name" | "age"
We will find out more about the keyof operator later in this chapter when we learn about lookup types.