Skip to content

交叉类型

将多个类型合并为一个类型,它包含了所需的所有类型的特性。

typescript
interface Foo {
  foo: string
}
interface Bar {
  bar: string
}

type Baz = Foo & Bar
const baz: Baz = {
  foo: 'foo',
  bar: 'bar'
}
ts
interface Foo {
  name: string
}
interface Bar {
  name: number
}
type Baz = Foo & Bar // never
ts
const mixin = <T extends object, K extends object> (source: T, target: K): T & k {
  return { ...source, ...target }
}

const res = mixin({ a: 1 }, { b: 2 })