//Arrays in TypeScript must contain values of the same type. When declaring an array, the type needs to be declared. let arrayName: number[] = [10,9,8]; //What if the array needs to hold values of different types? Now, we need a tuple. A tuple is a special structure in TypeScript that can hold as many values as needed of different types. let tupleName: [number, string, number]; tupleName = [10, "9", 8]; // JS=let cargoHold = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket']; let cargoHold: string[] = ['oxygen tanks', 'space suits', 'parrot', 'instruction manual', 'meal packs', 'slinky', 'security blanket']; //JS= let element1 = ['hydrogen', 'H', 1.008]; let element2 = ['helium', 'He', 4.003]; let element26 = ['iron', 'Fe', 55.85]; //TS= let element1: [string, string, number]; element1 = ['hydrogen', 'H', 1.008]; let element2: [string, string, number]; element2 = ['helium', 'He', 4.003]; let element26: [string, string, number]; element26 = ['iron', 'Fe', 55.85];
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter