oxc-project/oxc · error · OxcDiagnostic
Compare with `undefined` directly instead of using `typeof`.
Error message
Compare with `undefined` directly instead of using `typeof`.
What it means
Raised by unicorn/no-typeof-undefined when code tests for undefined with `typeof x === 'undefined'`. Except for potentially undeclared variables, a direct comparison is clearer; the faulting input is the typeof-based comparison at the labeled span (an autofix usually rewrites it).
Source
Thrown at crates/oxc_linter/src/rules/unicorn/no_typeof_undefined.rs:18
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use oxc_syntax::operator::{BinaryOperator, UnaryOperator};
use schemars::JsonSchema;
use serde::Deserialize;
use crate::{
AstNode,
ast_util::get_declaration_of_variable,
context::LintContext,
fixer::{RuleFix, RuleFixer},
rule::{DefaultRuleConfig, Rule},
};
fn no_typeof_undefined_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Compare with `undefined` directly instead of using `typeof`.")
.with_label(span)
}
#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct NoTypeofUndefined {
/// If set to `true`, also report `typeof x === "undefined"` when `x` may be a global
/// variable that is not declared (commonly checked via `typeof foo === "undefined"`).
check_global_variables: bool,
}
declare_oxc_lint!(
/// ### What it does
///
/// Disallow `typeof` comparisons with `undefined`.
///
/// ### Why is this bad?
///View on GitHub (pinned to e1e7af627c)
Solutions
- Compare directly: `x === undefined` or `x === void 0`
- Keep `typeof` only when the variable may be genuinely undeclared and that must be tolerated
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/no_typeof_undefined.rs:18 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/d36a1b9620ce476a.
Report an issue: GitHub.