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

A destructured parameter in TypeScript is a function parameter that utilizes JavaScript's destructuring assignment syntax to unpack object properties or array elements directly within the function signature, coupled with a TypeScript type annotation to enforce the shape of the unpacked data.

In TypeScript, the destructuring pattern and the type annotation must be declared separately. The type annotation applies to the entire parameter object or array, not to the individual destructured variables.

## Object Destructuring Syntax

The standard syntax requires defining the destructuring pattern first, followed by a colon, and then the type literal or interface.

```typescript theme={"dark"}
function processUser({ name, age }: { name: string; age: number }): void {
    console.log(name, age);
}
```

**Crucial Syntax Distinction:**
A common error is attempting to inline the type annotations directly within the destructuring pattern. In JavaScript, a colon inside a destructuring pattern indicates variable renaming, not type assignment.

```typescript theme={"dark"}
// INCORRECT: This renames the property 'name' to a local variable called 'string'.
// It results in an implicit 'any' type error in strict mode.
function processUserIncorrect({ name: string, age: number }) { }

// CORRECT: Renaming a variable while providing the correct type annotation.
function processUserCorrect({ name: userName }: { name: string }): void {
    console.log(userName);
}
```

## Using Type Aliases and Interfaces

To prevent function signatures from becoming overly verbose, the type annotation is typically extracted into a `type` alias or `interface`.

```typescript theme={"dark"}
interface UserPayload {
    name: string;
    age: number;
    isActive?: boolean;
}

function processUserPayload({ name, age, isActive }: UserPayload): void {
    // ...
}
```

## Default Values

Default values can be assigned at two distinct levels within a destructured parameter: at the property level and at the parameter level.

**1. Property-level defaults:**
Assigned within the destructuring pattern. The type annotation must mark these properties as optional (`?`) if the caller is allowed to omit them.

```typescript theme={"dark"}
function configure({ host = "localhost", port = 80 }: { host?: string; port?: number }): void {
    // ...
}
```

**2. Parameter-level defaults:**
Assigned after the type annotation. This prevents a `TypeError` if the function is called with no arguments at all.

```typescript theme={"dark"}
function configureWithFallback({ host = "localhost", port = 80 }: { host?: string; port?: number } = {}): void {
    // ...
}

configureWithFallback(); // Valid. Defaults to {} which then triggers property defaults.
```

## Array Destructuring Syntax

Array destructured parameters follow the same principle but utilize tuple types for the annotation to ensure positional type safety.

```typescript theme={"dark"}
function setCoordinates([x, y, z]: [number, number, number?]): void {
    // ...
}
```

## Rest Elements in Destructured Parameters

The rest operator (`...`) can be used to gather remaining properties or elements. The type annotation must account for the types of these remaining properties.

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

function initialize({ id, ...options }: Config): void {
    // 'id' is typed as string
    // 'options' is implicitly typed as { retries: number; timeout: number; }
}
```

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