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

Basic const Type Parameter Syntax

How to declare a type parameter with the const modifier to request narrowest possible inference.

Basic const Type Parameter Syntax

What you'll learn: How to declare a type parameter with the const modifier to get the narrowest possible type inference from TypeScript.


When you pass arguments to generic functions, TypeScript usually widens the inferred types to be more flexible. For example, an array literal ['hello', 'world'] might be inferred as string[] instead of the tuple ['hello', 'world']. An object { x: 10 } becomes { x: number } instead of { x: 10 }.

This widening is often helpful, but sometimes you want TypeScript to capture the exact, most specific type of what you passed—just like using as const on a value.


The const Modifier on Type Parameters

TypeScript lets you add the const modifier directly to a type parameter:

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

When you use <const T>, TypeScript infers T as narrowly as possible, treating the argument as if it were declared with as const.

const result = identity(['a', 'b', 'c']);
// Inferred as: readonly ['a', 'b', 'c']
// NOT as: string[]

Without const, the same function would infer string[].


How It Works

The const modifier tells TypeScript: "lock down this type parameter to the literal types, tuple shapes, and readonly constraints that would result from as const."

function processConfig<const T>(config: T) {
  return config;
}

const myConfig = processConfig({ apiKey: 'secret', timeout: 3000 });
// Type: { readonly apiKey: 'secret'; readonly timeout: 3000 }

Key Takeaway: The const type parameter modifier requests the narrowest possible inference, treating arguments as if you had applied as const—useful when you need to preserve exact literals and readonly tuple/object shapes.