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

In TypeScript, the `?:` syntax is not a single operator or contiguous token, but rather the sequential pairing of the Optional Modifier (`?` or `QuestionToken`) and the Type Annotation Separator (`:` or `ColonToken`). This sequence is primarily used in structural declarations to mark a member as optional while simultaneously defining its type. In expression and type-level contexts, the `?` and `:` tokens operate independently as components of conditional logic.

**Optional Modifier and Type Annotation**
In property, parameter, and tuple declarations, the `?` token marks the preceding entity as optional. The `:` token following it introduces the type annotation. Because they are distinct tokens in the Abstract Syntax Tree (AST), they can legally be separated by whitespace.

The placement of the `?` token depends strictly on the declaration context:

* Immediately after an **identifier** (in classes, interfaces, parameters, and labeled tuples).
* Immediately after the **closing bracket** of an index signature (in mapped types).
* Immediately after the **type itself** (in unlabeled tuple elements).

While the `:` and subsequent type annotation can be omitted in class properties, interface properties, and function parameters (defaulting to `any`), they are strictly required in mapped types and labeled tuple elements. Omitting the `:` in these contexts results in a syntax error.

Mechanically, the TypeScript compiler implicitly unions the explicitly declared type with `undefined`. If the `exactOptionalPropertyTypes` compiler option is enabled, the property must either be omitted entirely or set to the exact declared type, rejecting explicit `undefined` assignments unless `| undefined` is manually appended to the type definition.

```typescript theme={"dark"}
// Classes and Interfaces (Identifier followed by ? and optional :)
interface Entity {
  id? : string;     // Whitespace is legal between ? and :
  untypedProperty?; // : and type annotation are optional here (resolves to any | undefined)
}

// Mapped Types (Placed after the index signature closing bracket; : is strictly required)
type OptionalMapped<T> = {
  [K in keyof T]?: T[K];
};

// Function Parameters (Must appear after all required parameters)
function execute(param?: number): void {}

// Labeled Tuple Elements (Identifier followed by ?; : is strictly required)
type Coordinate = [x: number, y: number, z?: number];

// Unlabeled Tuple Elements (Placed immediately after the type itself)
type StringNumberTuple = [string, number?];
```

**Conditional Contexts**
When the `?` and `:` tokens appear in expression contexts rather than declaration contexts, they function as delimiters for conditional branching.

**Conditional Types (Type System)**
At the type level, `?` and `:` form a conditional type. This construct selects one of two possible types based on an assignability relationship evaluated using the `extends` keyword. The `?` precedes the true branch, and the `:` precedes the false branch.

```typescript theme={"dark"}
// Syntax Template
type TrueType = { isTrue: true };
type FalseType = { isTrue: false };
type ConditionalType<T, U> = T extends U ? TrueType : FalseType;

// Type Inference Example
type IsString<T> = T extends string ? true : false;
type A = IsString<"hello">; // Resolves to: true
```

**Conditional (Ternary) Operator (Runtime Logic)**
At runtime, `?` and `:` form the ternary operator, evaluating an expression-based `if...else` statement. The TypeScript type checker infers the return type of the entire expression as a union of the true and false operands' types, subject to standard type widening rules based on the assignment target.

```typescript theme={"dark"}
declare const condition: boolean;

// Inferred as literal union: "stringVal" | 42
const result = condition ? "stringVal" : 42; 

// Widened to: string | number
let mutableResult = condition ? "stringVal" : 42;
```

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