no-import-assign
Disallow assigning to imported bindings
Using the recommended
config from @eslint/js
in a configuration file enables this rule
The updates of imported bindings by ES Modules cause runtime errors.
Rule Details
This rule warns the assignments, increments, and decrements of imported bindings.
Examples of incorrect code for this rule:
/*eslint no-import-assign: "error"*/
import mod, { named } from "./mod.mjs"
import * as mod_ns from "./mod.mjs"
// ERROR: 'mod' is readonly.
// ERROR: 'named' is readonly.
// ERROR: The members of 'mod_ns' are readonly.
// ERROR: 'mod_ns' is readonly.
// Can't extend 'mod_ns'
// ERROR: The members of 'mod_ns' are readonly.
Examples of correct code for this rule:
/*eslint no-import-assign: "error"*/
import mod, { named } from "./mod.mjs"
import * as mod_ns from "./mod.mjs"
mod.prop = 1
named.prop = 2
mod_ns.named.prop = 3
// Known Limitation
function test(obj) {
obj.named = 4 // Not errored because 'obj' is not namespace objects.
}
test(mod_ns) // Not errored because it doesn't know that 'test' updates the member of the argument.
When Not To Use It
If you don’t want to be notified about modifying imported bindings, you can disable this rule.
Handled by TypeScript
It is safe to disable this rule when using TypeScript because TypeScript's compiler enforces this check.
Note that the compiler will not catch the Object.assign()
case. Thus, if you use Object.assign()
in your codebase, this rule will still provide some value.
Version
This rule was introduced in ESLint v6.4.0.