详解 TypeScript 中的 typeof 和 keyof
keyof
interface Point {
a: string;
b: string;
}
type Pk = keyof Point;
const age: Pk = 'b';
type OptionsFlags<T> = {
[key in keyof T]: boolean;
};
type Name = OptionsFlags<{ name: 1; age: 11 }>;
// type Name = {
// name: boolean;
// age: boolean;
// }
keyof 作用于 object 的类型,并将其 key 值转换为联合类型
typeof
用户获取变量的类型
function showName() {
return {
name: 133,
};
}
type P = ReturnType<typeof showName>;
// type P = {
// name: number;
// };
const info = ['school', 'age'] as const;
type Info = typeof info[number];
// type Info = "school" | "age"