是什么?

typeof 用于获取变量的类型,或者变量的类型,所以 typeof 后面始终是跟着一个变量。

  1. interface Person {
  2. name: string;
  3. age: number;
  4. isChina: boolean;
  5. }
  6. let me: Person = {
  7. name: 'stone',
  8. age: 18,
  9. isChina: true
  10. }
  11. type handsome = typeof me;
  12. // type handsome = Person
  1. interface Person {
  2. name: string;
  3. age: number;
  4. isChina: boolean;
  5. }
  6. let me: Person = {
  7. name: 'stone',
  8. age: 18,
  9. isChina: true
  10. }
  11. type handsome = typeof me;
  12. // type handsome = Person
  13. const colorObj = {
  14. red: 'red',
  15. yellow: 'yellow',
  16. };
  17. type colorType = keyof typeof colorObj;
  18. // type colorType = "red" | "yellow"
  19. let color: colorType;
  20. // let color: "red" | "yellow"
  21. color = 'red';
  22. color = 'yellow';
  23. // color = 'blue';
  24. // 不能将类型“"blue"”分配给类型“"red" | "yellow"”。ts(2322)