Skip to content

Files

Latest commit

 

History

History
134 lines (99 loc) · 2.11 KB

no-useless-undefined.md

File metadata and controls

134 lines (99 loc) · 2.11 KB

Disallow useless undefined

💼 This rule is enabled in the ✅ recommended config.

🔧 This rule is automatically fixable by the --fix CLI option.

undefined is the default value for new variables, parameters, return statements, etc… so specifying it doesn't make any difference.

The only case where passing undefined is required is due to bad TypeScript types in functions, in which case you can use checkArguments: false option.

Fail

let foo = undefined;
const {foo = undefined} = bar;
const noop = () => undefined;
function foo() {
	return undefined;
}
function* foo() {
	yield undefined;
}
function foo(bar = undefined) {
}
function foo({bar = undefined}) {
}
foo(undefined);

Pass

let foo;
const {foo} = bar;
const noop = () => {};
function foo() {
	return;
}
function* foo() {
	yield;
}
function foo(bar) {
}
function foo({bar}) {
}
foo();

Options

Type: object

checkArguments

Type: boolean
Default: true

Disallow the use of undefined at the end of function call arguments. Pass checkArguments: false to disable checking them.

Fail

// eslint unicorn/no-useless-undefined: ["error", {"checkArguments": true}]
foo(bar, baz, undefined);

Pass

// eslint unicorn/no-useless-undefined: ["error", {"checkArguments": false}]
foo(bar, baz, undefined);

Conflict with ESLint array-callback-return rule

We recommend setting the ESLint array-callback-return rule option allowImplicit to true:

{
	"rules": {
		"array-callback-return": [
			"error",
			{
				"allowImplicit": true
			}
		]
	}
}