oxc-project/oxc · error · OxcDiagnostic

Invalid `typeof` comparison value.

Error message

Invalid `typeof` comparison value.

What it means

The `invalid_value` branch of oxlint's `valid-typeof`: the typeof expression is compared to a string literal, but the literal is not one of the eight values typeof can return ("undefined", "object", "boolean", "number", "string", "function", "symbol", "bigint") — typically a typo.

Source

Thrown at crates/oxc_linter/src/rules/eslint/valid_typeof.rs:26

use serde::Deserialize;

use crate::{
    AstNode,
    context::LintContext,
    rule::{DefaultRuleConfig, Rule},
};

fn not_string(help: Option<&'static str>, span: Span) -> OxcDiagnostic {
    let mut d =
        OxcDiagnostic::warn("`typeof` comparisons should be to string literals.").with_label(span);
    if let Some(x) = help {
        d = d.with_help(x);
    }
    d
}

fn invalid_value(help: Option<String>, span: Span) -> OxcDiagnostic {
    let mut d = OxcDiagnostic::warn("Invalid `typeof` comparison value.").with_label(span);
    if let Some(x) = help {
        d = d.with_help(x);
    }
    d
}

#[derive(Debug, Clone, Default, JsonSchema, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct ValidTypeof {
    /// The `requireStringLiterals` option when set to `true`, allows the comparison of `typeof`
    /// expressions with only string literals or other `typeof` expressions, and disallows
    /// comparisons to any other value. Default is `false`.
    ///
    /// With `requireStringLiterals` set to `true`, the following are examples of **incorrect** code:
    /// ```js
    /// typeof foo === undefined
    /// typeof bar == Object
    /// typeof baz === "strnig"

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Correct the literal to one of the eight valid typeof results
  2. Use `Array.isArray(x)` for array checks instead of typeof
  3. Cross-check with `tsc --noEmit`: TypeScript rejects typeof comparisons with impossible literals

Example fix

// before — "str" is not a possible typeof result
const isStr = typeof value === "str";

// after
const isStr = typeof value === "string";
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json
{ "rules": { "valid-typeof": "error" } }
// dual gate: npx tsc --noEmit && npx oxlint --deny-warnings src/

Prevention

When it happens

Trigger: `typeof foo === "str"`, `typeof x !== "interger"`, `typeof f === "fuction"` — any misspelled or impossible typeof result literal on either side of the comparison.

Common situations: Typos in type-guard helper functions; wrong casing ("Number" vs "number"); made-up names like "array" written from memory; older code never checked by a compiler.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/b839e0176fdeebea. Report an issue: GitHub.