类型别名

  1. type Plustype = (x: number, y: number) => number;
  2. let sum: Plustype = (x: number, y: number) => {
  3. return x + y;
  4. };
  5. type stringOrNumber = string | number;
  6. let a: stringOrNumber = 123;
  7. let b: stringOrNumber = "abc";

字面量类型

  1. let a: "up" | "down" | "left" | "right"
  2. type Direction = "up" | "down" | "left" | "right"
  3. let b: Direction

交叉类型

  1. interface IName {
  2. name: string;
  3. }
  4. interface IAge {
  5. age: number;
  6. }
  7. type IPerson = IName & IAge; // 这个有点像继承多个接口的意思
  8. let zhangSan: IPerson = {
  9. name: "zhangSan",
  10. age: 18,
  11. };
  12. class Lisi implements IName, IAge {
  13. name: "lisi";
  14. age: 18;
  15. }