Skip to main content

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.

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.
// 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.
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.
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.
// 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.
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.
Master TypeScript with Deep Grasping Methodology!Learn More