# no-import-assign

不允许分配给导入的绑定

配置文件中的 "extends": "eslint:recommended" 属性启用了该规则

ES 模块对导入绑定的更新会导致运行时错误。

# 规则详情

此规则警告导入绑定的分配、增量和减量。

此规则的错误代码示例:

/*eslint no-import-assign: "error"*/

import mod, { named } from "./mod.mjs"
import * as mod_ns from "./mod.mjs"

mod = 1          // ERROR: 'mod' is readonly.
named = 2        // ERROR: 'named' is readonly.
mod_ns.named = 3 // ERROR: The members of 'mod_ns' are readonly.
mod_ns = {}      // ERROR: 'mod_ns' is readonly.
// Can't extend 'mod_ns'
Object.assign(mod_ns, { foo: "foo" }) // ERROR: The members of 'mod_ns' are readonly.

此规则的正确代码示例:

/*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.

# 何时不使用

如果您不想收到有关修改导入绑定的通知,您可以禁用此规则。

Last Updated: 5/13/2023, 8:55:38 PM