---
url: /docs/guide/usage/linter/rules/vitest/prefer-expect-type-of.md
---

### 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.

### 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

## Version

This rule was added in v1.44.0.

## References
