Destructuring Tuples
Part of the Introduction To TypeScript section of Coddy's JavaScript journey — lesson 17 of 73.
While accessing tuple elements by index works perfectly, there's an even more elegant way to extract values from tuples: destructuring. This JavaScript feature works seamlessly with TypeScript's typed tuples, allowing you to unpack tuple values into separate variables in a single line.
Array destructuring uses square brackets on the left side of an assignment to create new variables that correspond to each position in the tuple:
let userInfo: [number, string] = [42, "Alice"];
let [id, name] = userInfo;
console.log(id); // 42 (TypeScript knows this is a number)
console.log(name); // "Alice" (TypeScript knows this is a string)This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
Challenge
EasyCreate a tuple named employee that holds a number (employee ID) followed by a string (employee name). Initialize it with the ID 1001 and the name "Sarah Johnson".
Use destructuring to extract the values from the employee tuple into two separate variables named empId and empName.
Create another tuple named coordinates that holds two numbers representing latitude and longitude coordinates. Initialize it with the values 40.7128 and -74.0060.
Use destructuring to extract the values from the location tuple into two separate variables named latitude and longitude.
Print the destructured variables in the following order, each on a separate line:
- Print the employee ID
- Print the employee name
- Print the latitude
- Print the longitude
Cheat sheet
Destructuring allows you to extract values from tuples into separate variables in a single line using square brackets:
let userInfo: [number, string] = [42, "Alice"];
let [id, name] = userInfo;
console.log(id); // 42 (TypeScript knows this is a number)
console.log(name); // "Alice" (TypeScript knows this is a string)TypeScript maintains type information for each destructured variable based on the tuple's type definition.
Try it yourself
// TODO: Write your code here
// Create the employee tuple with ID 1001 and name "Sarah Johnson"
// Use destructuring to extract empId and empName
// Create the coordinates tuple with values 40.7128 and -74.0060 (avoid using 'location' as variable name)
// Use destructuring to extract latitude and longitude
// Print all four variables in the specified order
// Print the results (uncomment and modify as needed)
// console.log(empId);
// console.log(empName);
// console.log(latitude);
// console.log(longitude);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