• 联合类型:多种数据类型之一(或)
  • 交叉类型:同时满足多种类型(且)

联合类型

  1. let arr: (number | string)[] = [1,2,'a','b']
  2. // 类型别名
  3. type ID = (number | string)
  4. let id1: ID = 1
  5. let id2: ID = '2'
  6. // 只能是 数字1 或字符串"1"
  7. let n: 1 | "1";
  8. n = 1;
  9. n = "1";

交叉类型

  1. let user: { name: string; age: number } & { sex: string };
  2. user = {
  3. name: "zhangsan",
  4. age: 12,
  5. sex: "男",
  6. };