Sub topic 10
Const type parameters in generic functions
This chapter assumes you know basic TypeScript types, unions, literal types, arrays, object types, typeof, and keyof. It also assumes you understand that as const preserves literal types and adds readonly.
A const type parameter is a generic type parameter that asks TypeScript to infer narrow, literal types for an argument. It provides behavior similar to adding as const at the call site, but the generic function defines that behavior for its callers.
The problem with ordinary generic inference
Consider a generic function that returns an array:
function tuple<T extends readonly unknown[]>(items: T): T {
return items;
}
const value = tuple(["id", 1]);
// often inferred as readonly ["id", 1] only when the constraint and context preserve itWith ordinary generic inference, TypeScript may widen values during inference. A string such as "id" can become string, and an array can become a general array type instead of a tuple.
A tuple is an array with a fixed number of positions and a known type at each position. The type ["id", 1] means that the first item is exactly "id" and the second item is exactly 1.
TypeScript 5.0 added the const modifier for type parameters:
function tuple<const T extends readonly unknown[]>(items: T): T {
return items;
}
const value = tuple(["id", 1]);
// readonly ["id", 1]The const modifier tells the compiler to prefer a const-like inference for an inline argument. The function returns the same narrow type that an as const assertion would usually produce.
Preserving literal types in a helper
This is useful when a helper builds configuration data, routes, commands, or other values from literals.
function defineRoute<
const T extends {
path: string;
method: "GET" | "POST";
}
>(route: T): T {
return route;
}
const route = defineRoute({
path: "/users",
method: "GET",
});
route.path;
// "/users"
route.method;
// "GET"The constraint checks that the object has a path property and that method is either "GET" or "POST". The const modifier preserves the specific values "/users" and "GET" instead of widening them to string and "GET" | "POST".
This lets later types use the exact information from the value:
type RoutePath = typeof route.path;
// "/users"
type RouteMethod = typeof route.method;
// "GET"The function still returns a normal JavaScript object. The const modifier changes type inference only.
Constraints still matter
A const type parameter does not replace a useful constraint. The constraint defines which arguments are valid and which operations are safe inside the function.
function makePair<const T extends readonly [string, number]>(value: T): T {
return value;
}
const pair = makePair(["user-id", 42]);
// readonly ["user-id", 42]
// makePair(["user-id"]);
// error: source has only one element
// makePair([99, "user-id"]);
// error: the first element must be a stringThe constraint readonly [string, number] requires exactly two elements in a particular order. The const modifier then preserves the exact values supplied by the caller.
Use a readonly constraint when the function only reads the value. This allows both readonly tuples and mutable arrays to satisfy the constraint.
Inline arguments versus existing variables
Const inference works best for literals written directly in the function call.
function capture<const T extends readonly unknown[]>(items: T): T {
return items;
}
const direct = capture(["a", 1]);
// readonly ["a", 1]
const existing = ["a", 1];
const fromVariable = capture(existing);
// (string | number)[]
// The variable was already widened before the call.The declaration of existing gives it a mutable array type. Once the values have widened to string and number, the generic function cannot recover the original literal types.
You can preserve the type when declaring the variable:
const existing = ["a", 1] as const;
const fromVariable = capture(existing);
// readonly ["a", 1]The difference is important. A const type parameter controls inference at a function boundary. It cannot undo widening that happened earlier.
A practical route helper
A route registry can use const inference to preserve every route's literal fields:
type Route = {
path: string;
method: "GET" | "POST";
};
function routes<const T extends readonly Route[]>(items: T): T {
return items;
}
const apiRoutes = routes([
{ path: "/users", method: "GET" },
{ path: "/users", method: "POST" },
]);
type Path = (typeof apiRoutes)[number]["path"];
// "/users"
type Method = (typeof apiRoutes)[number]["method"];
// "GET" | "POST"The expression [number] means “the type of any element in this tuple or array.” Indexing the resulting object type with "path" or "method" extracts that property type.
The runtime array and its compile-time types now come from one declaration. Adding another route changes both automatically.
When to use a const type parameter
Use a const type parameter when you own a generic helper and want callers to retain literal information without writing as const themselves.
Use as const when you are declaring one particular value and want to control its type directly.
Use a normal type parameter when widening is useful or when the function should treat several literal values as one broader type. Narrow inference can make types more precise, but it can also make them more complex.
Exercise
What type does const result = tuple(["x", 10]) produce when tuple is declared as function tuple<const T extends readonly unknown[]>(items: T): T?
Show answer
readonly ["x", 10], because the const type parameter preserves the inline argument's literal types and tuple structure.