unicorn/no-useless-undefined

Disallow undefined where leaving it out means the same

fixable

A variable without a value, a return without a value and a missing argument are already undefined, and a parameter defaults to undefined anyway. Writing it out changes nothing.

--fix removes the undefined.

Configure#

Off by default. To turn it on:

bunfig.toml
[lint.rules]
"unicorn/no-useless-undefined" = "error" # or "warn"

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/no-useless-undefined" = ["error", {
  checkArguments = true,
  checkArrowFunctionBody = true,
}]

Examples#

example.js5 problems
let selected = undefined;

function find(id, fallback = undefined) {
  if (!id) {
    return undefined;
  }
  return load(id, undefined);
}

const noop = () => undefined;
example.jsNo problems
let selected;

function find(id, fallback) {
  if (!id) {
    return;
  }
  return load(id);
}

const noop = () => {};

Sometimes you do need to write it. A const must have a value. A function with a return type keeps its return undefined, because TypeScript may require it. And you can only drop undefined at the end of the arguments.

example.tsNo problems
const missing = undefined;

function find(id: string): string | undefined {
  return undefined;
}

render(undefined, container);

Calls where the argument is the point aren't reported either. That covers functions whose name starts with set, collection methods such as push, add, has and includes, assertions such as toBe and toEqual, createContext, and ref.

example.jsNo problems
setUser(undefined);
items.push(undefined);
expect(result).toBe(undefined);

Options#

OptionDefaultWhat it does
checkArgumentstrueReport undefined at the end of a call's arguments.
checkArrowFunctionBodytrueReport () => undefined.
example.jswith { "checkArguments": false, "checkArrowFunctionBody": false }No problems
load(id, undefined);

const noop = () => undefined;