Untitled

interface 求交集

interface Colorful {
  color: string
}
interface Circle {
  radius: number
}
type ColorfulCircle = Colorful & Circle // 接口也能求交集

const cc: ColorfulCircle = {
  color: 'blue',
  radius: 23
}

type 模拟继承

type Person = {
  name: string
  age: number
}
type User = Person & {
  id: number
  email: string
}
const u: User = {
  id: 1,
  name: 'Lance',
  age: 18,
  email: '[email protected]'
}

属性冲突求交集

type Person = {
  id: string
  name: string
  age: number
}
type User = Person & {
  id: number
  email: string
}
const u: User = {
  id: 1, // never 属性冲突,求的是交集
  name: 'Lance',
  age: 18,
  email: '[email protected]'
}

interface 扩展

interface Person {
  id: string
  name: string
  age: number
}
interface User extends Person {
  id: number
  email: string
}

Untitled