Untitled

简单类型

Untitled

普通对象的兼容

Untitled

type Person = {
  name: string
  age: number
}
let user = {
  name: 'Lance',
  age: 18,
  id: 1,
  email: '[email protected]'
}
let p: Person
p = user
let user = {
  name: 'Lance',
  age: 18,
  id: 1,
  email: '[email protected]'
}
type User = typeof user

Untitled

父子接口类型兼容

Untitled

复杂的函数兼容

Untitled

let fn1 = (a: number) => {
  console.log(a);
}
let fn2 = (b: number, s: string) => {
  console.log(b, s);
}
fn2 = fn1 // OK
fn1 = fn2 // 报错