# Const Type Parameters

> Inferring the most narrow, literal type automatically (TS 5.0).

Subject: TypeScript | Lesson 26 of 54 | 4 min read
Interactive lesson: https://codewithanimation.in/typescript/const-type-parameters
Previous: [Multiple Generics & Defaults](https://codewithanimation.in/typescript/multiple-generics-defaults.md) | Next: [Indexed Access Types](https://codewithanimation.in/typescript/indexed-access-types.md)

## The <const T> modifier

Introduced in TypeScript 5.0, you can add the `const` modifier to a generic type parameter.

This forces TypeScript to infer the most narrow, literal type possible for whatever argument is passed, exactly as if the caller had written `as const`.

## Improving Developer Experience

Before TS 5.0, if you wanted a function to remember the exact values of an array (e.g., `["alice", "bob"]` instead of `string[]`), you had to beg the caller to add `as const` to their argument.

With `<const T>`, the burden shifts from the caller to the function author. The caller just passes a normal array, and the function automatically infers a strict, readonly tuple.

## Syntax

```typescript
// 1. Without const: Infers wide types
function getRoutes<T>(routes: T) { return routes; }
const r1 = getRoutes(["/home", "/about"]); 
// r1 is typed as string[]

// 2. With const: Infers exact literal tuples
function getRoutesConst<const T>(routes: T) { return routes; }
const r2 = getRoutesConst(["/home", "/about"]);
// r2 is strictly typed as readonly ["/home", "/about"]
```

## Common mistake: It doesn't make the variable mutable

Wait, it infers a `readonly` tuple? Yes. The `const` generic modifier infers the type *as if* it were declared with `as const`.

This means the inferred type is strictly read-only. You cannot mutate `r2` inside the function or after it returns.

## Tip: Not a silver bullet

`const` type parameters only work when the argument is written *inline* inside the function call.

If the caller defines the array in a separate variable first, TypeScript will have already inferred it as `string[]` before it ever reaches your `<const T>` function!

## Try it: Try it

Toggle the `const` modifier on the generic parameter to see how TypeScript drastically changes its inference engine from a wide `string[]` array to a strict, literal tuple.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/typescript/const-type-parameters

## Check yourself

### Question 1

What happens if a user passes `const myArr = [1, 2]; myFunction(myArr);` to a function defined as `function myFunction<const T>(arr: T)`?

- A. T is inferred as readonly [1, 2]
- B. T is inferred as number[]
- C. TypeScript throws a compile error.

Correct answer: B

The <const T> modifier only narrows inline literals. If the variable is defined beforehand without 'as const', it's already a wide type (number[]) by the time it hits the function.

- Why not A: Because the array was defined in a separate variable first, it was already inferred as number[] before being passed.
- Why not C: It accepts it, it just falls back to the wider type.

## Remember this

- Use `<const T>` to automatically infer literal types and readonly tuples (TS 5.0+).
- It acts exactly as if the caller had appended `as const` to their inline argument.
- It improves API ergonomics by not forcing users to write `as const`.
- It only works on arguments defined inline at the call site.
