default-param-last
Enforce default parameters to be last.
此规则扩展了基本 eslint/default-param-last
规则。
它增加了对可选参数的支持。
英:This rule extends the base eslint/default-param-last
rule.
It adds support for optional parameters.
- ❌ 不正确
- ✅ 正确
/* eslint @typescript-eslint/default-param-last: "error" */
function f(a = 0, b: number) {}
function f(a: number, b = 0, c: number) {}
function f(a: number, b?: number, c: number) {}
class Foo {
constructor(
public a = 10,
private b: number,
) {}
}
class Foo {
constructor(
public a?: number,
private b: number,
) {}
}
Open in Playground/* eslint @typescript-eslint/default-param-last: "error" */
function f(a = 0) {}
function f(a: number, b = 0) {}
function f(a: number, b?: number) {}
function f(a: number, b?: number, c = 0) {}
function f(a: number, b = 0, c?: number) {}
class Foo {
constructor(
public a,
private b = 0,
) {}
}
class Foo {
constructor(
public a,
private b?: number,
) {}
}
Open in Playground如何使用
.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"default-param-last": "off",
"@typescript-eslint/default-param-last": "error"
}
};
选项
参见 eslint/default-param-last
选项。
资源
摘自 ❤️ ESLint 内核