Skip to content

Const Type Parameter

In one line

<const T> makes type parameter inference use literal types instead of widening — like as const for generics.

In simple words

The const modifier on a type parameter (TypeScript 5.0+) tells TypeScript to infer the narrowest possible type for that parameter — literal types instead of widened base types.

Without const: function first<T>(arr: T[]): T called with ["a", "b"] infers T = string. With const: function first<const T>(arr: readonly T[]): T infers T = "a" | "b".

This is like as const but for generic type parameters — it applies the narrow inference automatically without the caller needing to add as const.

Requirements: the parameter must be readonly (because literal types are immutable). This prevents accidental mutation of the inferred literal type.

Example

typescript
// Without const type parameter — T widens to string
function first<T>(arr: readonly T[]): T {
  return arr[0];
}
const r1 = first(["a", "b", "c"]); // type: string (widened)

// With const type parameter — T stays as literal union
function firstConst<const T>(arr: readonly T[]): T {
  return arr[0];
}
const r2 = firstConst(["a", "b", "c"]); // type: "a" | "b" | "c" (narrow)

// Practical: tuple inference
function pair<const T>(first: T, second: T): [T, T] {
  return [first, second];
}
const p = pair("hello", "world"); // type: ["hello" | "world", "hello" | "world"]

<const T> prevents widening: the array ['a', 'b', 'c'] infers T as 'a' | 'b' | 'c' instead of string. Like as const but automatic for generic parameters.

Common confusions

  • Confused with: as const

    The difference: as const is applied by the caller on a specific value. const type parameter is applied by the function author on the generic declaration. as const requires the caller to remember; const type parameter is automatic. They achieve the same narrow inference but from different sides.

Related terms

Related Resources

Related Lessons

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to glossary