default-param-last

强制默认参数在最后

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

最后放置默认参数允许函数调用省略可选的尾参数。

¥Putting default parameter at last allows function calls to omit optional tail arguments.

// Correct: optional argument can be omitted
function createUser(id, isAdmin = false) {}
createUser("tabby")

// Incorrect: optional argument can **not** be omitted
function createUser(isAdmin = false, id) {}
createUser(undefined, "tabby")

规则详情

¥Rule Details

此规则强制默认参数是最后一个参数。

¥This rule enforces default parameters to be the last of parameters.

此规则的错误代码示例:

¥Examples of incorrect code for this rule:

在线运行
/* eslint default-param-last: ["error"] */

function f(a = 0, b) {}

function g(a, b = 0, c) {}

此规则的正确代码示例:

¥Examples of correct code for this rule:

在线运行
/* eslint default-param-last: ["error"] */

function f(a, b = 0) {}

function g(a, b = 0, c = 0) {}

此规则还支持 TypeScript 类型语法。

¥This rule additionally supports TypeScript type syntax.

此规则的错误 TypeScript 代码示例:

¥Examples of incorrect TypeScript code for this rule:

/* eslint default-param-last: ["error"] */

function h(a = 0, b: number) {}

此规则的正确 TypeScript 代码示例:

¥Examples of correct TypeScript code for this rule:

/* eslint default-param-last: ["error"] */

function h(a = 0, b?: number) {}

版本

此规则是在 ESLint v6.4.0 中引入。

资源