Using const with Array Parameters
What you'll learn: How to use the const type parameter modifier to infer array arguments as readonly tuples with precise literal types instead of widening to mutable arrays.
When you pass an array to a generic function, TypeScript normally infers it as a mutable array type, widening literals to their base types. With const type parameters, you can preserve the exact structure and literal values as a readonly tuple.
The Problem Without const
When you pass an array literal to a generic function, TypeScript infers it as a mutable array of the widened type:
function printArray<T>(arr: T[]) {
return arr;
}
const result = printArray(['red', 'green', 'blue']);
// result is inferred as string[], not ['red', 'green', 'blue']
This loses precision — the specific string literals and exact length are gone.
Using const Type Parameters
The const modifier on a type parameter tells TypeScript to infer the narrowest possible type:
function printArray<const T>(arr: T[]) {
return arr;
}
const result = printArray(['red', 'green', 'blue']);
// result is inferred as readonly ['red', 'green', 'blue']
Now TypeScript knows the exact tuple type with literal values, and marks it as readonly.
Why This Matters
This pattern is powerful for configuration objects, route definitions, and anywhere you want precise literal inference:
function createConfig<const T>(items: T[]) {
return items;
}
const colors = createConfig(['primary', 'secondary']);
// colors is readonly ['primary', 'secondary']
type ColorType = typeof colors[number];
// ColorType is 'primary' | 'secondary'
The readonly guarantee prevents accidental mutations, and the tuple structure preserves ordering and length information that you can extract with utility types.
Key Takeaway: The const type parameter modifier forces arrays to be inferred as readonly tuples with precise literal types, giving you maximum type safety and information preservation.