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 declaration in TypeScript defines a block-scoped variable with a constant reference to a value. Once initialized, the identifier cannot be reassigned to a new memory location. However, const enforces binding immutability, not value immutability; the internal properties of objects and arrays assigned to a const variable remain mutable at runtime.
Syntax and Initialization
Aconst variable must be initialized at the exact moment of its declaration. It cannot be declared and assigned later.
TypeScript Type Inference (Literal Types)
While JavaScript treatsconst purely as a runtime constraint, the TypeScript compiler uses const to alter type inference. When a primitive value is assigned to a const variable, TypeScript infers a literal type rather than widening it to a general primitive type.
Scope and Temporal Dead Zone (TDZ)
Likelet, const is block-scoped, meaning it only exists within the nearest enclosing {} block. It is also subject to the Temporal Dead Zone (TDZ), meaning the variable cannot be accessed before its line of declaration is executed.
Reference vs. Value Immutability
Reassigning the identifier throws a compiler error, but mutating the properties of a complex data structure (like an object or array) is perfectly valid.Const Assertions (as const)
To enforce deep immutability at the type level and prevent type widening for objects and arrays, TypeScript provides the as const assertion. This casts all properties to readonly and infers the narrowest possible literal types.
Master TypeScript with Deep Grasping Methodology!Learn More





