You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

31 lines
570 B

2 years ago
  1. // 使用接口来描述对象的类型,达到复用的目的
  2. // 接口:只能为对象指定类型
  3. interface IPerson {
  4. name: string
  5. age: number
  6. sayHi(): void
  7. }
  8. // 对象使用接口
  9. let Iperson: IPerson = {
  10. name: 'Ken',
  11. age: 20,
  12. sayHi() {}
  13. }
  14. // type(类型别名):可以为对象指定类型,还可以任意类型指定别名
  15. type IPerson2 = {
  16. name: string
  17. age: number
  18. sayHi(): void
  19. }
  20. let Iperson2: IPerson2 ={
  21. name: 'Ken',
  22. age: 20,
  23. sayHi() {}
  24. }
  25. // 任意类型指定别名
  26. type NumStr = number | string