Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

TypeScript · Function

Const Type Parameters

A const type parameter makes const-like inference the default for eligible inline object, array, and primitive expressions. It helps generic APIs preserve literal properties and tuple types without requiring callers to write as const.

Since
TypeScript 5.0

How it works

A const modifier on a type parameter makes inference prefer const-like, precise types for object, array, and primitive expressions written directly in a call. It can preserve literal properties and tuples without requiring callers to add as const, while the constraint still describes what the function may use.

Syntax

function getNamesExactly<const T extends HasNames>(arg: T): T["names"] { return arg.names; }

Parameters

NameTypeDescription
Tconst type parameterGeneric type inferred with const-like rules for eligible inline expressions; it is constrained by the declared extends constraint when one is present.
argTThe value from which T is inferred.

Returns

The example returns T["names"], preserving the inferred names property type.

Key points

  • TypeScript 5.0 introduced the const modifier on type parameter declarations.
  • The modifier changes the default inference strategy toward narrower literal types.
  • An inline array in the documented example is inferred as a readonly string-literal tuple.
  • Callers can often obtain precise literal inference without adding as const to the argument.
  • The modifier affects inference and does not act as a prohibition against mutable arguments.
  • For a previously inferred mutable array variable, adding the modifier to the function does not change the variable's existing type.

Common mistakes

  • A const type parameter does not force callers to pass immutable values; it changes inference rather than enforcing readonly inputs.
  • Prefer readonly constraints when the inferred value is intended to retain readonly tuple or array information.
  • Values stored in variables before the call may not receive const-like inference from the modifier.
  • The modifier has no effect where an argument expression could not benefit from an as const-style transformation.

Example: Preserve literal tuple inference

type HasNames = { names: readonly string[] };

function getNamesExactly<const T extends HasNames>(arg: T): T["names"] {
  return arg.names;
}

const names = getNamesExactly({ names: ["Alice", "Bob", "Eve"] });
// readonly ["Alice", "Bob", "Eve"]
console.log(names);

The const type parameter preserves literal tuple information when the object literal is passed directly.

When to use it

Literal-preserving configuration APIs

Generic helpers that accept inline configuration objects and should retain exact property literals.

Tuple-preserving array helpers

Array utilities where inline arguments should infer as readonly tuples of literal elements.

Ergonomic library interfaces

Public generic functions that should provide precise inference without requiring callers to annotate every argument.

Interview questions

QWhat does a const type parameter do?

It is written by placing const before the type parameter name, such as function f<const T>(value: T): T. This makes const-like inference the default for eligible expressions passed directly to the generic call.

Follow-up: How would you preserve a tuple while restricting its elements? Use a readonly constraint, for example function f<const T extends readonly string[]>(value: T): T.

QWhat kinds of expressions are affected?

It improves inference precision for object, array, and primitive expressions written in the call. For example, an inline string array can be inferred as a readonly tuple instead of a widened mutable array.

Follow-up: Does it make the parameter immutable at runtime? No; it only affects the inferred type and does not reject mutable values.

QWhy might passing a variable not preserve literal types?

A variable such as const arr = ["a", "b"] is already inferred as string[] unless otherwise annotated. Passing that variable later does not retroactively apply const-like inference to its original initializer.

Follow-up: How can the caller retain literal information? Use an inline expression, an explicit type annotation, or as const when appropriate.

  • const assertions
  • literal types
  • type parameter
  • generic functions
  • type parameter constraints
  • readonly arrays
  • tuples

Source: Documentation - TypeScript 5.0. Checked 2026-10-15.