const with Object Parameters
What you'll learn: How const type parameters make object properties infer as readonly and as narrow literal types instead of widening to general types.
The Problem Without const
When you pass an object to a generic function, TypeScript normally widens the property types. A string literal "admin" becomes string. A numeric literal 42 becomes number. This is usually helpful for flexibility, but sometimes you want to preserve the exact literal values.
How const Type Parameters Work
Adding const before a type parameter tells TypeScript: "Infer this as narrowly as possible, as if the entire value were marked with as const."
When applied to object parameters, two key things happen:
- Properties become
readonly— TypeScript infers them as immutable - Literal values stay literal —
"admin"stays"admin", notstring
function createConfig<const T>(config: T) {
return config;
}
const settings = createConfig({
role: "admin",
port: 3000
});
// settings is inferred as:
// {
// readonly role: "admin"; // literal, not string
// readonly port: 3000; // literal, not number
// }
Without const, role would be string and port would be number.
Why This Matters
This pattern is powerful for configuration objects, route definitions, and anywhere you want exact literal types preserved for type safety—like discriminated unions or lookup tables—without manually writing as const at every call site.
Key Takeaway: const type parameters automatically apply deep literal narrowing and readonly to object arguments, giving you as const behavior without explicit annotations.