Labeled Tuple Types

Labeled Tuple Types

Updated: January 16, 2021
TypeScript Programming

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

Say you now have a function printAddress which takes an Address type as its arg.

function printAddress(...address: Address) {
    // ...stuff
}

Your editor (before TS@4) would hint that the arguments were something like:

address_0: number, address_1: string, ...etc

which is not really helpful because address_0: number doesn’t really explain what that parameter corresponds to. With Labled Tuple Types we can do

type Address = [streetNumber: number, city: string, state: string, zip: number]

NOTE: once you begin to label an element in a type definition, you must label all of the other elements.

Now you get a hint like:

streetNumber: number, city: string, ...etc

which is crystal clear.