Stable bindings
A value such as a configuration reference, service instance, or imported dependency should not be rebound after setup.
TypeScript · Const
TypeScript’s const creates a block-scoped binding that must be initialized and cannot be reassigned. The referenced object or array may still be mutable unless additional type-level or runtime measures are used.
Use const for a binding that should not be reassigned. It shares let's block scoping, requires initialization, and does not make an object or array deeply immutable; use readonly or a const assertion when type-level immutability or literal precision is needed.
const variableName = initializer;const settings = { retries: 3 };
settings.retries = 4;
// settings = {}; // Error: cannot reassign a const binding
console.log(settings.retries);The binding cannot be reassigned, but the referenced object can still be mutated.
A value such as a configuration reference, service instance, or imported dependency should not be rebound after setup.
A declaration does not need to change during its scope, making least-privilege intent explicit.
An object or array remains intentionally mutable while its reference stays stable.
const creates a block-scoped binding that must be initialized and cannot later be assigned a new value. It does not freeze the value held by that binding.
Follow-up: How would you preserve literal types in an object? Use an as const assertion, which also makes object properties readonly.
Use const when a declaration will not be reassigned; use let when its binding must change. Both follow block-scoping rules, unlike var's function-scoped behavior.
Follow-up: Does const make an object deeply immutable? No; use readonly types or a const assertion for type-level restrictions.
A const declaration must receive its initial value at the declaration site, and assigning to its binding afterward is an error. The binding can still refer to mutable state.
Follow-up: What does as const change? It prevents literal widening and gives object properties readonly types while producing readonly tuples for arrays.
letvarreadonlyas const