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

An optional parameter in TypeScript is a function, method, or constructor parameter that is not strictly required during invocation. By appending a question mark (`?`) to the parameter identifier in the signature, the TypeScript compiler permits the caller to omit the argument, implicitly passing `undefined` in its place.

```typescript theme={"dark"}
function functionName(requiredParam: Type, optionalParam?: Type): ReturnType {
    // Implementation
}
```

## Type System Behavior

When a parameter is marked as optional, TypeScript automatically unions its declared type with `undefined`. Assuming `--strictNullChecks` is enabled in the `tsconfig.json`, a parameter declared as `param?: string` is evaluated by the type checker as `string | undefined`.

Consequently, the compiler enforces type narrowing within the function body, requiring you to handle the `undefined` state before invoking methods specific to the declared type.

```typescript theme={"dark"}
function parseInput(data: string, encoding?: string): void {
    // 'encoding' is typed as 'string | undefined'
    
    // Compiler Error: Object is possibly 'undefined'
    // let len = encoding.length; 
    
    if (encoding !== undefined) {
        // Type narrowed to 'string'
        let len = encoding.length; 
    }
}

parseInput("payload");           // Valid: encoding is omitted
parseInput("payload", "utf-8");  // Valid: encoding is provided
```

## Structural Constraints

TypeScript enforces strict positional rules for optional parameters. All optional parameters must appear after all required parameters in the parameter list. A required parameter cannot follow an optional parameter.

```typescript theme={"dark"}
// Valid: Optional parameter follows required parameter
function validSignature(a: number, b?: string): void {}

// Invalid: Required parameter follows optional parameter
// Compiler Error: A required parameter cannot follow an optional parameter.
function invalidSignature(a?: number, b: string): void {}
```

## Interaction with Default Parameters

Parameters assigned a default value via an initializer (`=`) are inherently optional to the caller. However, you cannot combine the optional modifier (`?`) with a default initializer.

```typescript theme={"dark"}
// Valid: Implicitly optional due to default initializer
function withDefault(a: number, b: string = "default"): void {}

// Invalid: Parameter cannot have question mark and initializer
// Compiler Error: Parameter cannot have question mark and initializer.
function conflictingSyntax(a: number, b?: string = "default"): void {}
```

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