Skip to main content
Destructuring in TypeScript is a syntax that allows unpacking values from arrays or properties from objects into distinct, statically-typed variables. TypeScript enhances standard JavaScript destructuring by enforcing structural typing, enabling explicit type annotations, and automatically inferring types from the source structure.

Object Destructuring

When destructuring objects, TypeScript infers the types of the extracted variables based on the source object’s shape.

Explicit Typing vs. Renaming

A critical syntactic distinction in TypeScript is that a colon (:) inside the destructuring pattern denotes variable renaming, not a type annotation. Type annotations must be applied to the entire destructured object.

Array and Tuple Destructuring

Array destructuring extracts values based on positional indices. TypeScript applies strict typing based on whether the source is an array (homogeneous) or a tuple (heterogeneous, fixed-length).

Default Values

Default values can be assigned to destructured variables to handle undefined. TypeScript infers the variable’s type as a union of the default value’s type and the property’s defined type, narrowing out undefined.

Rest Elements (...)

The rest operator extracts all remaining un-destructured properties or elements into a new variable. TypeScript dynamically computes the type of the rest variable by omitting the explicitly destructured keys.

Function Parameter Destructuring

Destructuring is commonly applied directly within function signatures. The type annotation must follow the destructured parameter object.
Tired of Poor TypeScript Skills? Fix That With Deep Grasping!Learn More