{"record":{"id":"d36a1b9620ce476a","repo":"oxc-project/oxc","slug":"compare-with-undefined-directly-instead-of-using","errorCode":null,"errorMessage":"Compare with `undefined` directly instead of using `typeof`.","messagePattern":"Compare with `undefined` directly instead of using `typeof`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/unicorn/no_typeof_undefined.rs","lineNumber":18,"sourceCode":"use oxc_ast::{AstKind, ast::Expression};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\nuse oxc_syntax::operator::{BinaryOperator, UnaryOperator};\nuse schemars::JsonSchema;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    ast_util::get_declaration_of_variable,\n    context::LintContext,\n    fixer::{RuleFix, RuleFixer},\n    rule::{DefaultRuleConfig, Rule},\n};\n\nfn no_typeof_undefined_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Compare with `undefined` directly instead of using `typeof`.\")\n        .with_label(span)\n}\n\n#[derive(Debug, Default, Clone, JsonSchema, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct NoTypeofUndefined {\n    /// If set to `true`, also report `typeof x === \"undefined\"` when `x` may be a global\n    /// variable that is not declared (commonly checked via `typeof foo === \"undefined\"`).\n    check_global_variables: bool,\n}\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Disallow `typeof` comparisons with `undefined`.\n    ///\n    /// ### Why is this bad?\n    ///","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/no_typeof_undefined.rs#L1-L36","documentation":"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).","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/unicorn/no_typeof_undefined.rs:18 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Compare directly: `x === undefined` or `x === void 0`","Keep `typeof` only when the variable may be genuinely undeclared and that must be tolerated"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}