Course contentsShow
← TypeScript
Lesson 113 of 1,7963. Basic TypesFree lesson

Array Destructuring with Types

Type inference and annotation patterns when destructuring array elements.

Array Destructuring with Types

What you'll learn: How TypeScript infers and checks types when you destructure array elements into separate variables.

The Core Idea

Array destructuring lets you unpack array elements into individual variables in one line. TypeScript tracks the type of each position in the array and applies those types to your destructured variables automatically — but you can also annotate them explicitly when needed.

Think of it like unpacking a labeled box: if TypeScript knows what's in positions 0, 1, and 2 of your array, it automatically labels each variable you unpack with the correct type.

Type Inference with Destructuring

When you destructure a typed array, TypeScript infers types based on the array's element type:

const numbers: number[] = [10, 20, 30];
const [first, second] = numbers;
// first: number, second: number

For arrays with union types, each destructured variable gets that union:

const mixed: (string | number)[] = ["hello", 42];
const [greeting, count] = mixed;
// greeting: string | number, count: string | number

Explicit Type Annotations

You can explicitly type destructured variables when you need to be specific:

const values = [100, 200];
const [x, y]: [number, number] = values;

This is especially useful when the array type is inferred too broadly and you want to confirm the structure.

With Rest Parameters

Destructuring works with rest elements, which capture remaining items as an array:

const items: number[] = [1, 2, 3, 4];
const [head, ...tail] = items;
// head: number, tail: number[]

Key Takeaway: TypeScript automatically infers types for destructured array elements based on the array's type, and you can add explicit annotations when you need tighter control over the types being unpacked.