TypeScript const Type Parameters Preserve Literal Types Without as const Assertions

TypeScript's const type parameter modifier allows generic functions to infer the narrowest possible type from arguments, eliminating the need for callers to add as const assertions. Without this feature, passing a literal like { method: "GET" } to a generic function causes TypeScript to widen the type to { method: string }, losing precision that downstream code may depend on. By adding const to a type parameter in the function signature, the compiler automatically retains exact string literals, readonly tuples, and deeply readonly object properties. Unlike as const, which is a caller-side annotation prone to inconsistent adoption and ineffective across library boundaries, const parameters encode the immutability requirement directly in the function signature. The feature is particularly useful in configuration builders, API route definitions, and discriminated union factories where literal types drive control flow.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.
Discussion (0)
Log in to join the discussion and vote.
Log in