oxc-project/oxc · error · OxcDiagnostic
Type assertions should be done using `toBeTypeOf`.
Error message
Type assertions should be done using `toBeTypeOf`.
What it means
Emitted by the vitest/prefer-expect-type-of rule at an expectation that asserts a type with `typeof x === 'string'` (or similar comparison) instead of vitest's `toBeTypeOf` matcher. It fires because `toBeTypeOf` is the idiomatic, more readable vitest type assertion.
Source
Thrown at crates/oxc_linter/src/rules/vitest/prefer_expect_type_of.rs:16
use oxc_ast::{
AstKind,
ast::{Argument, UnaryOperator},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};
use crate::{
context::LintContext,
rule::Rule,
utils::{MemberExpressionElement, PossibleJestNode, parse_expect_jest_fn_call},
};
fn prefer_expect_type_of_diagnostic(span: Span, help: &str) -> OxcDiagnostic {
OxcDiagnostic::warn("Type assertions should be done using `toBeTypeOf`.")
.with_help(format!("Substitute the assertion with `{help}`."))
.with_label(span)
.with_note("https://vitest.dev/api/expect#tobetypeof")
}
#[derive(Debug, Default, Clone)]
pub struct PreferExpectTypeOf;
declare_oxc_lint!(
/// ### What it does
///
/// Enforce using [`toBeTypeOf`](https://vitest.dev/api/expect#tobetypeof) instead of `expect(typeof ...).toBe(...)`.
///
/// ### Why is this bad?
///
/// `expect(typeof value).toBe(type)` works but is awkward and produces poor failure messages.
/// Vitest's built-in `toBeTypeOf` matcher performs the same `typeof` comparison with a clearer API and better error output.
///View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `expect(typeof x).toBe('string')` / `expect(typeof x === 'string').toBe(true)` with `expect(x).toBeTypeOf('string')`
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/vitest/prefer_expect_type_of.rs:16 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/1f84a6ceb50f2287.
Report an issue: GitHub.