Skip to content

constantAssignments

Reports attempting to reassign variables declared with const.

✅ This rule is included in the ts untyped presets.

The const keyword creates a read-only reference to a value, preventing reassignment. While properties of const objects and elements of const arrays can be mutated, the binding itself cannot be reassigned. Attempting to reassign a const variable results in a runtime error.

const
const value: 1
value
= 1;
value = 2;
Error ts(2588) ― Cannot assign to 'value' because it is a constant.
const
const result: any
result
=
const getValue: any
getValue
();
result =
const getOtherValue: any
getOtherValue
();
Error ts(2588) ― Cannot assign to 'result' because it is a constant.
const
const counter: 0
counter
= 0;
counter++;
Error ts(2588) ― Cannot assign to 'counter' because it is a constant.
const {
const property: any
property
} =
const object: any
object
;
property = "new value";
Error ts(2588) ― Cannot assign to 'property' because it is a constant.
for (const
const item: any
item
of
const items: any
items
) {
item =
const processItem: any
processItem
(
const item: any
item
);
Error ts(2588) ― Cannot assign to 'item' because it is a constant.
}

This rule is not configurable.

If your codebase relies on non-standard behavior that involves reassigning constant variables (which is highly discouraged), you might choose to disable this rule. For example, if you target a legacy runtime with non-standard JavaScript semantics, standard practices may not apply to you.

Made with ❤️‍🔥 around the world by the Flint team and contributors.