max-params
Enforce a maximum number of parameters in function definitions.
🧱
This is an "extension" rule that replaces a core ESLint rule to work with TypeScript. See Rules > Extension Rules.
This rule extends the base max-params
rule from ESLint core. 它增加了对 TypeScript this
参数的支持,因此它们不会被视为参数。
¥It adds support for TypeScript this
parameters so they won't be counted as a parameter.
如何使用
- Flat Config
- Legacy Config
eslint.config.mjs
export default tseslint.config({
rules: {
// Note: you must disable the base rule as it can report incorrect errors
"max-params": "off",
"@typescript-eslint/max-params": "error"
}
});
.eslintrc.cjs
module.exports = {
"rules": {
// Note: you must disable the base rule as it can report incorrect errors
"max-params": "off",
"@typescript-eslint/max-params": "error"
}
};
在线运行试试这个规则 ↗
选项
See eslint/max-params
's options.
¥Options
该规则添加了以下选项:
¥This rule adds the following options:
interface Options extends BaseMaxParamsOptions {
countVoidThis?: boolean;
}
const defaultOptions: Options = {
...baseMaxParamsOptions,
countVoidThis: false,
};
countVoidThis
Whether to count a this
declaration when the type is void
. Default: false
.
countVoidThis
设置为 false
且 max
为 1
时的代码示例:
¥Example of a code when countVoidThis
is set to false
and max
is 1
:
- ❌ 错误
- ✅ 正确
function hasNoThis(this: void, first: string, second: string) {
// ...
}
Open in Playgroundfunction hasNoThis(this: void, first: string) {
// ...
}
Open in Playground