oxc-project/oxc · error · OxcDiagnostic
`typeof` comparisons should be to string literals.
Error message
`typeof` comparisons should be to string literals.
What it means
oxlint's `valid-typeof` rule (eslint, correctness) requires `typeof` expressions to be compared only against string literals (or other typeof expressions). This message comes from `not_string` and fires when the other side of the comparison is not a string literal — nearly always a missing pair of quotes.
Source
Thrown at crates/oxc_linter/src/rules/eslint/valid_typeof.rs:18
use schemars::JsonSchema;
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span, best_match};
use oxc_syntax::operator::UnaryOperator;
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`View on GitHub (pinned to e1e7af627c)
Solutions
- Quote the expected value: `typeof x === "undefined"`
- If the comparison target must be dynamic, turn off `requireStringLiterals` or compare two typeof expressions
- Run `npx tsc --noEmit` as well — TypeScript flags non-overlapping typeof comparisons
Example fix
// before — compares against the identifier undefined, not the string if (typeof callback === undefined) return; // after if (typeof callback === "undefined") return;
Defensive patterns
Strategy: validation
Validate before calling
// .oxlintrc.json
{ "rules": { "valid-typeof": "error" } }
// dual gate: npx tsc --noEmit && npx oxlint --deny-warnings src/ Prevention
- Always quote typeof comparison targets; treat `typeof x === undefined` as a red flag in review
- Enable the eslint correctness category so valid-typeof runs by default
- Run tsc alongside oxlint — the compiler also catches non-overlapping typeof comparisons
- Prefer direct `x === undefined` checks over typeof when the binding is known to exist
When it happens
Trigger: `typeof foo === undefined` (comparing against the identifier), `typeof bar === 1`, or — with `requireStringLiterals: true` — comparisons against variables or any non-literal expression.
Common situations: The classic `typeof x === undefined` bug where the developer means `"undefined"`; comparisons against constants holding type names when requireStringLiterals is enabled; copy-pasted feature-detection guards.
Related errors
- Checking inequality with NaN will always return true
- Checking equality with NaN will always return false
- Comparison with NaN will always return false
- Invalid `typeof` comparison value.
- Empty array binding pattern
AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20).
Data as JSON: /api/errors/8021b4ef981f1d0f.
Report an issue: GitHub.