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

A `const` type parameter is a modifier applied to a generic type parameter declaration that instructs the TypeScript compiler to infer the most specific literal type possible from a passed argument, rather than widening it to its base primitive or mutable type.

By prefixing a generic parameter with the `const` keyword, you alter the compiler's default type inference algorithm for that specific generic context.

## Syntax

The `const` modifier is placed directly before the type parameter name inside the angle brackets. It can be used standalone or combined with `extends` constraints.

```typescript theme={"dark"}
// Standard const type parameter
function extract<const T>(arg: T): T {
  return arg;
}

// Const type parameter with a constraint
function extractArray<const T extends readonly unknown[]>(arg: T): T {
  return arg;
}
```

## Inference Mechanics

When TypeScript resolves generic type arguments, its default behavior is to widen literal values to their base types to allow for mutability. The `const` modifier suppresses this widening process, applying a deep `readonly` inference model equivalent to appending an `as const` assertion at the call site.

### 1. Primitive Widening

Without the modifier, literal primitives are widened. With the modifier, the exact literal type is preserved.

```typescript theme={"dark"}
function standard<T>(val: T) { return val; }
function strict<const T>(val: T) { return val; }

const a = standard("data"); // Inferred as: string
const b = strict("data");   // Inferred as: "data"
```

### 2. Object and Array Inference

For structural types, the `const` modifier recursively applies `readonly` modifiers to all properties and infers arrays as `readonly` tuples.

```typescript theme={"dark"}
const obj1 = standard({ id: 1, tags: ["a", "b"] });
// Inferred as: { id: number, tags: string[] }

const obj2 = strict({ id: 1, tags: ["a", "b"] });
// Inferred as: { readonly id: 1, readonly tags: readonly ["a", "b"] }
```

## Technical Constraints and Rules

* **Call-Site Exclusivity:** The `const` modifier only affects types inferred from arguments passed at the call site. It has no effect on the internal implementation of the function or class.
* **Explicit Type Arguments Override:** If a developer explicitly provides a type argument during invocation, the `const` modifier's inference rules are bypassed entirely.

```typescript theme={"dark"}
// The explicit <string> overrides the const literal inference
const c = strict<string>("data"); // Inferred as: string
```

* **Non-Literal Arguments:** If the argument passed is already a widened variable (e.g., a variable declared with `let`), the `const` type parameter cannot infer a literal type because the literal information has already been lost prior to the function call.

```typescript theme={"dark"}
let dynamicValue = "data";
const d = strict(dynamicValue); // Inferred as: string
```

* **Class and Interface Generics:** The `const` modifier is only valid on type parameters for functions, methods, and classes where inference from arguments occurs. It cannot be used on interface or type alias declarations.

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