Skip to content
← Back to rules

vitest/prefer-expect-type-of Style

🛠️ An auto-fix is available for this rule.

What it does ​

Enforce using 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.

Examples ​

Examples of incorrect code for this rule:

js
test("type checking", () => {
  expect(typeof "hello").toBe("string");
  expect(typeof 42).toBe("number");
  expect(typeof true).toBe("boolean");
  expect(typeof {}).toBe("object");
  expect(typeof (() => {})).toBe("function");
  expect(typeof Symbol()).toBe("symbol");
  expect(typeof 123n).toBe("bigint");
  expect(typeof undefined).toBe("undefined");
});

Examples of correct code for this rule:

js
test("type checking", () => {
  expect("hello").toBeTypeOf("string");
  expect(42).toBeTypeOf("number");
  expect(true).toBeTypeOf("boolean");
  expect({}).toBeTypeOf("object");
  expect(() => {}).toBeTypeOf("function");
  expect(Symbol()).toBeTypeOf("symbol");
  expect(123n).toBeTypeOf("bigint");
  expect(undefined).toBeTypeOf("undefined");
});

How to use ​

To enable this rule using the config file or in the CLI, you can use:

json
{
  "plugins": ["vitest"],
  "rules": {
    "vitest/prefer-expect-type-of": "error"
  }
}
ts
import { defineConfig } from "oxlint";

export default defineConfig({
  plugins: ["vitest"],
  rules: {
    "vitest/prefer-expect-type-of": "error",
  },
});
bash
oxlint --deny vitest/prefer-expect-type-of --vitest-plugin

Version ​

This rule was added in v1.44.0.

References ​