Literal-preserving configuration APIs
Generic helpers that accept inline configuration objects and should retain exact property literals.
TypeScript · Function
A const type parameter makes const-like inference the default for eligible inline object, array, and primitive expressions. It helps generic APIs preserve literal properties and tuple types without requiring callers to write as const.
A const modifier on a type parameter makes inference prefer const-like, precise types for object, array, and primitive expressions written directly in a call. It can preserve literal properties and tuples without requiring callers to add as const, while the constraint still describes what the function may use.
function getNamesExactly<const T extends HasNames>(arg: T): T["names"] { return arg.names; }| Name | Type | Description |
|---|---|---|
T | const type parameter | Generic type inferred with const-like rules for eligible inline expressions; it is constrained by the declared extends constraint when one is present. |
arg | T | The value from which T is inferred. |
The example returns T["names"], preserving the inferred names property type.
const modifier on type parameter declarations.as const to the argument.as const-style transformation.type HasNames = { names: readonly string[] };
function getNamesExactly<const T extends HasNames>(arg: T): T["names"] {
return arg.names;
}
const names = getNamesExactly({ names: ["Alice", "Bob", "Eve"] });
// readonly ["Alice", "Bob", "Eve"]
console.log(names);The const type parameter preserves literal tuple information when the object literal is passed directly.
Generic helpers that accept inline configuration objects and should retain exact property literals.
Array utilities where inline arguments should infer as readonly tuples of literal elements.
Public generic functions that should provide precise inference without requiring callers to annotate every argument.
It is written by placing const before the type parameter name, such as function f<const T>(value: T): T. This makes const-like inference the default for eligible expressions passed directly to the generic call.
Follow-up: How would you preserve a tuple while restricting its elements? Use a readonly constraint, for example function f<const T extends readonly string[]>(value: T): T.
It improves inference precision for object, array, and primitive expressions written in the call. For example, an inline string array can be inferred as a readonly tuple instead of a widened mutable array.
Follow-up: Does it make the parameter immutable at runtime? No; it only affects the inferred type and does not reject mutable values.
A variable such as const arr = ["a", "b"] is already inferred as string[] unless otherwise annotated. Passing that variable later does not retroactively apply const-like inference to its original initializer.
Follow-up: How can the caller retain literal information? Use an inline expression, an explicit type annotation, or as const when appropriate.
const assertionsliteral typestype parametergeneric functionstype parameter constraintsreadonly arraystuples