Course contentsShow
← TypeScript
Lesson 1414 of 1,79632. Advanced PatternsFree lesson

What const Type Parameters Solve

Understanding the narrowing problem with generic inference and why const type parameters were introduced in TypeScript 5.0.

What const Type Parameters Solve

You'll learn: Why TypeScript 5.0 introduced const type parameters to preserve literal types during generic inference.


The Narrowing Problem

Before TypeScript 5.0, generic type inference had a frustrating limitation: it would automatically widen literal types to their broader primitive types.

When you passed a literal value to a generic function, TypeScript would "forget" the specific value and remember only its general type:

function identity<T>(value: T) {
  return value;
}

const result = identity("hello");
// Type: string (not "hello")

const tuple = identity([1, 2, 3] as const);
// Type: readonly [1, 2, 3] ✓

const objResult = identity({ name: "Alice", role: "admin" });
// Type: { name: string; role: string } (not literals!)

Notice how "hello" became string, and the object properties widened from "Alice" and "admin" to string. This happened because TypeScript assumed you wanted a general type for flexibility.

The real problem: When you wanted those specific literal types preserved (like when building type-safe configurations or utilities), you had to manually add as const everywhere.


Why This Mattered

This widening behavior broke patterns that relied on precise literal types:

  • Type-safe event systems where event names were specific strings
  • Configuration objects where keys and values needed exact types
  • Builder patterns that tracked precise states
  • Discriminated unions generated from input data

You'd end up writing as const repeatedly, cluttering your code and making APIs harder to use.


The Solution: const Type Parameters

TypeScript 5.0 introduced const type parameters to tell the compiler: "infer this as narrowly as possible, as if the user wrote as const."

This modifier changes the inference behavior from widening to narrowing, preserving literal types automatically.

(Note: The actual syntax and usage of const type parameters will be covered in the next lesson.)


Key Takeaway: Const type parameters solve the generic inference narrowing problem by preserving literal types automatically, eliminating the need for manual as const assertions in function calls.