导致eslint no-shadow错误的解构参数
Destructured parameters causing eslint no-shadow error
我的理解是这样的解构将从传入的参数对象中选择属性:
const last = "Smith" // unrelated const
const full = function({ first, last }) {
return [first, last].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""
这似乎也适用于进口。
import last from "last"; // unrelated import
export function full({ first, last }) {
return [first, last].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""
为什么这会导致 no-shadow
eslint 错误?
error 'last' is already declared in the upper scope no-shadow
是否存在full
可以访问外部引用的情况?没有权利?在不重命名不相关的外部 last
引用的情况下,我如何使语法接近于此?
它导致了 eslint 错误,因为 last
在导入和函数中声明为参数。所以在函数内部,参数 last
隐藏了导入 last
。您只需要更改函数内的参数名称或关闭 eslint。
import last from "last"; // unrelated import
export function full({ first, last: lst }) {
return [first, lst].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""
...或者...
import last from "last"; // unrelated import
export function full({ first, last }) { // eslint-disable-line no-shadow
return [first, last].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""
我的理解是这样的解构将从传入的参数对象中选择属性:
const last = "Smith" // unrelated const
const full = function({ first, last }) {
return [first, last].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""
这似乎也适用于进口。
import last from "last"; // unrelated import
export function full({ first, last }) {
return [first, last].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""
为什么这会导致 no-shadow
eslint 错误?
error 'last' is already declared in the upper scope no-shadow
是否存在full
可以访问外部引用的情况?没有权利?在不重命名不相关的外部 last
引用的情况下,我如何使语法接近于此?
它导致了 eslint 错误,因为 last
在导入和函数中声明为参数。所以在函数内部,参数 last
隐藏了导入 last
。您只需要更改函数内的参数名称或关闭 eslint。
import last from "last"; // unrelated import
export function full({ first, last: lst }) {
return [first, lst].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""
...或者...
import last from "last"; // unrelated import
export function full({ first, last }) { // eslint-disable-line no-shadow
return [first, last].filter(v => v).join(" ")
}
full({last: "Doe"}) // "Doe"
full({}) // ""