> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# TypeScript Destructured Variable

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.

```typescript theme={"dark"}
interface Payload {
  code: number;
  message: string;
  timestamp: string;
}

const response: Payload = { code: 200, message: "OK", timestamp: "2023-10-01" };

// Inferred typing: 'code' is number, 'message' is string
const { code, message } = response;
```

### 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.

```typescript theme={"dark"}
// This renames the 'code' property to a new variable named 'responseCode'.
// TypeScript correctly infers the variable 'responseCode' as type 'number'.
const { code: responseCode } = response; 

// To apply explicit types while renaming, the type annotation must follow the entire destructuring pattern.
const { code: statusCode, message: msg }: { code: number; message: string } = response;
```

## 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).

```typescript theme={"dark"}
// Tuple destructuring
const matrix: [number, string, boolean] = [1, "active", true];

// TypeScript strictly types 'id' as number, 'status' as string
const [id, status] = matrix;

// Skipping elements maintains correct positional typing ('flag' is boolean)
const [, , flag] = matrix;
```

## 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`.

```typescript theme={"dark"}
interface Config {
  timeout?: number;
  retries?: number;
}

const settings: Config = { timeout: 5000 };

// 'retries' is typed as 'number' (not 'number | undefined') due to the default value
const { timeout, retries = 3 }: Config = settings;
```

## 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.

```typescript theme={"dark"}
// Object Rest
const { timestamp, ...coreData } = response;
// coreData is typed as: { code: number; message: string; }

// Array Rest
const sequence: number[] = [10, 20, 30, 40];
const [head, ...tail] = sequence;
// tail is typed as: number[]
```

## Function Parameter Destructuring

Destructuring is commonly applied directly within function signatures. The type annotation must follow the destructured parameter object.

```typescript theme={"dark"}
// Destructuring with an external interface
function initialize({ timeout, retries }: Config): void {
  // Implementation
}

// Destructuring with inline typing
function updateCoordinates([x, y]: [number, number]): void {
  // Implementation
}
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor TypeScript Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
