What is a Tuple?
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 15 of 73.
A tuple is an array with a fixed number of elements where the type of each element at each position is known and defined.
Think of a tuple as a container with labeled slots. Unlike a regular array where you might have string[] (any number of strings), a tuple specifies exactly how many elements it contains and what type each element should be.
// Regular array - any number of strings
let names: string[] = ["Alice", "Bob", "Charlie"];
// Tuple - exactly 2 elements: string, then number
let person: [string, number] = ["Alice", 25];The key difference is that tuples have a fixed structure. The first element must always be a string, the second must always be a number, and there can be no third element.
Try it yourself
This lesson doesn't include a code challenge.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Introduction To TypeScript
1Getting Started with TS
What is TypeScript?Why Use TypeScript?Your First TypeScript CodeCompilation Process & ErrorsRecap: Introduction to TS4Working with Functions
Typing Params & Return ValuesTyping Arrow FunctionsThe 'void' Return TypeOptional Parameters with '?'Default Parameter ValuesTyping Rest ParametersDefining Function TypesRecap: Building Typed Funcs2Core Types
Basic Types: str, num, booleanThe 'any' Type: Escape HatchThe 'unknown' TypeWorking with 'null' & 'undef'Type Inference in ActionExplicit Type AnnotationsRecap: Core Types Practice5Types: Aliases, Unions & Inter
Type Aliases for PrimitivesUnion Types ('|')Working with Union TypesLiteral TypesIntersection Types ('&')Combining Type AliasesRecap: Advanced Type Combos3Data Structure: Arrays & Tuple
Typed Arrays'readonly' Modifier for ArraysWhat is a Tuple?Declaring and Accessing TuplesDestructuring TuplesReadonly TuplesMulti-dimensional Typed Arrays Spread Operator with ArraysRecap: Arrays and Tuples