ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
const type parameter is a modifier applied to a generic type parameter declaration that instructs the TypeScript compiler to infer the most specific literal type possible from a passed argument, rather than widening it to its base primitive or mutable type.
By prefixing a generic parameter with the const keyword, you alter the compiler’s default type inference algorithm for that specific generic context.
Syntax
Theconst modifier is placed directly before the type parameter name inside the angle brackets. It can be used standalone or combined with extends constraints.
Inference Mechanics
When TypeScript resolves generic type arguments, its default behavior is to widen literal values to their base types to allow for mutability. Theconst modifier suppresses this widening process, applying a deep readonly inference model equivalent to appending an as const assertion at the call site.
1. Primitive Widening
Without the modifier, literal primitives are widened. With the modifier, the exact literal type is preserved.2. Object and Array Inference
For structural types, theconst modifier recursively applies readonly modifiers to all properties and infers arrays as readonly tuples.
Technical Constraints and Rules
- Call-Site Exclusivity: The
constmodifier only affects types inferred from arguments passed at the call site. It has no effect on the internal implementation of the function or class. - Explicit Type Arguments Override: If a developer explicitly provides a type argument during invocation, the
constmodifier’s inference rules are bypassed entirely.
- Non-Literal Arguments: If the argument passed is already a widened variable (e.g., a variable declared with
let), theconsttype parameter cannot infer a literal type because the literal information has already been lost prior to the function call.
- Class and Interface Generics: The
constmodifier is only valid on type parameters for functions, methods, and classes where inference from arguments occurs. It cannot be used on interface or type alias declarations.
Master TypeScript with Deep Grasping Methodology!Learn More





