是什么?
typeof 用于获取变量的类型,或者变量的类型,所以 typeof 后面始终是跟着一个变量。
interface Person {
name: string;
age: number;
isChina: boolean;
}
let me: Person = {
name: 'stone',
age: 18,
isChina: true
}
type handsome = typeof me;
// type handsome = Person
interface Person {
name: string;
age: number;
isChina: boolean;
}
let me: Person = {
name: 'stone',
age: 18,
isChina: true
}
type handsome = typeof me;
// type handsome = Person
const colorObj = {
red: 'red',
yellow: 'yellow',
};
type colorType = keyof typeof colorObj;
// type colorType = "red" | "yellow"
let color: colorType;
// let color: "red" | "yellow"
color = 'red';
color = 'yellow';
// color = 'blue';
// 不能将类型“"blue"”分配给类型“"red" | "yellow"”。ts(2322)