no-unused-vars
注意: 此规则是
recommended
规则集的一部分。在
deno.json
中启用完整规则集{ "lint": { "tags": ["recommended"] } }
使用 Deno CLI 启用完整规则集
deno lint --tags=recommended
强制所有变量至少使用一次。
如果有变量被声明但未在任何地方使用,则很可能是由于不完整的重构。此 lint 规则检测并警告此类未使用的变量。
如果满足以下任一条件,则变量 a
被视为“已使用”
- 它的值被读取出来,例如
console.log(a)
或let otherVariable = a;
- 它被调用或构造,例如
a()
或new a()
- 它被导出,例如
export const a = 42;
如果一个变量只是被赋值,但从未被读取,则它被认为是“未使用”。
let a;
a = 42;
// `a` is never read out
如果您想有意声明未使用的变量,请在它们前面加上下划线字符 _
,例如 _a
。此规则会忽略以下划线 _
开头的变量。
无效
const a = 0;
const b = 0; // this `b` is never used
function foo() {
const b = 1; // this `b` is used
console.log(b);
}
foo();
let c = 2;
c = 3;
// recursive function calls are not considered to be used, because only when `d`
// is called from outside the function body can we say that `d` is actually
// called after all.
function d() {
d();
}
// `x` is never used
export function e(x: number): number {
return 42;
}
const f = "unused variable";
有效
const a = 0;
console.log(a);
const b = 0;
function foo() {
const b = 1;
console.log(b);
}
foo();
console.log(b);
let c = 2;
c = 3;
console.log(c);
function d() {
d();
}
d();
export function e(x: number): number {
return x + 42;
}
export const f = "exported variable";