类型别名

使用类型别名给类型起一个新的名字,使用 type 创建类型别名,常用于联合类型

ts
type Point = {
  x: number
  y: string
}

function foo7(x: Point) {
  console.log(x)
}
foo7({x: 123, y: '233'})

type Id = number | string
function foo8(id: Id) {
  console.log(id)
}
foo8(233)
foo8('233')

type cb = () => string
const fn:cb = (): string => 'olu'

type T = 'success' | 'error'
const res: T = 'success'

类型别名 type 不能通过同名方式去拓展, 可以使用&符号拓展合并

ts
type Animal1 = {
  name: string
}
type Bear1 = Animal1 & {
  age: number
}
const bear1: Bear1 = {
  name: 'weini',
  age: 23
}
console.log(bear1.name, bear1.age)

沿着联系继续阅读

0 条反向链接

引用本文

还没有公开笔记引用这里。

本文提到

暂时没有其他关联。

在花园里找一找

输入关键词开始搜索。按 Esc 关闭。