Skip to content
NewBun 1.4.5 is out. Release notes
AphrodyBun

Linter · Rules

unicorn/no-typeof-undefined

Require comparing with undefined directly instead of typeof x === "undefined"

typeof value === "undefined" is a long way to write value === undefined. You only need typeof for a name that may not be declared at all, where reading it throws a ReferenceError. That's why a variable that isn't declared in the file isn't reported by default.

--fix rewrites the comparison to === undefined or !== undefined. For a variable that isn't declared in the file, only --fix-suggestions does.

Configure

Off by default. To turn it on:

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

Every option, set to its default:

bunfig.toml
[lint.rules]
"unicorn/no-typeof-undefined" = ["error", {
  checkGlobalVariables = false,
}]

Examples

Reported:

function greet(name, options) {
  if (typeof name === "undefined") {
    name = "stranger";
  }
  return typeof options.loud !== "undefined";
}

Not reported:

function greet(name, options) {
  if (name === undefined) {
    name = "stranger";
  }
  return options.loud !== undefined;
}

// `window` is not declared in this file
const isServer = typeof window === "undefined";

Options

OptionDefaultWhat it does
checkGlobalVariablesfalseAlso report typeof on a variable that isn't declared in the file.
const isServer = typeof window === "undefined";