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

const vs Normal Type Parameters

Comparing inference behavior with and without const to see the difference in literal vs widened types.

const vs Normal Type Parameters

What you'll learn: How the const modifier on type parameters produces narrower, literal types instead of widened ones, giving you more precise inference.


The Core Difference

When TypeScript infers a type parameter, it normally widens literal types to their base types. For example, "hello" becomes string, and [1, 2] becomes number[].

The const modifier tells TypeScript: "preserve the most specific type possible" — just like the as const assertion does for values, but applied to generic type parameters.

Normal Type Parameter Behavior

Without const, TypeScript widens for flexibility:

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

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

const tuple = identity([1, 2, 3]);
// type: number[] (not [1, 2, 3])

The inferred type loses precision because TypeScript assumes you want a mutable, general type.

With const Type Parameters

Adding const preserves literal types:

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

const result = identityConst("hello");
// type: "hello" (literal string!)

const tuple = identityConst([1, 2, 3]);
// type: readonly [1, 2, 3] (literal tuple!)

Notice two things:

  1. String literals stay as literal types
  2. Arrays become readonly tuples with exact lengths

Why This Matters

Use const when you need:

  • Exact string literals for type safety (like configuration keys)
  • Tuple types instead of arrays (when order and length matter)
  • Object literals with precise property types
  • Maximum type narrowing from input values

Without const, you get flexibility but lose precision. With const, you get precision but values become readonly.


Key Takeaway: const type parameters prevent widening, preserving literal and tuple types instead of widening to string, number[], etc. — use it when you need the most specific type inference possible.