[TypeScript] Labelled tuple type

Let's see the unlabelled tuple type:

type Address = [
  number,
  string,
  string,
  number,
]

function printAddress(...address: Address) {
  console.log(address)
}

As you can see, the auto completion is not good.

 

Then see the labelled tuple types:

type Address_Label = [
  streetNumber: number,
  streeName: string,
  city: string,
  postCode: number,
]

function printAddress2(...address: Address_Label) {
  console.log(address)
}

Once you label one tuple type, you have to label all the tuple.

posted @ 2022-08-22 15:03  Zhentiw  阅读(12)  评论(0编辑  收藏  举报