# vitest/prefer-expect-type-of

> Require `toBeTypeOf` instead of `expect(typeof value).toBe(...)`

`expect(typeof value).toBe("string")` only gives `expect` the name of the type. When the test fails, the output compares `"number"` with `"string"` and doesn't show the value. `expect(value).toBeTypeOf("string")` shows it.

The rule reports `typeof` inside `expect` with `toBe` or `toEqual`, with or without `.not`. Despite its name, it asks for the `toBeTypeOf` matcher, not for `expectTypeOf()`.

`--fix` rewrites the assertion.

{/* Generated by src/codegen/lint-rule-docs.ts. Do not edit this section. */}

## Configure

Off by default. To turn it on:

```toml bunfig.toml
[lint.rules]
"vitest/prefer-expect-type-of" = "error" # or "warn"
```

{/* End of the generated section. */}

## Examples

Reported:

{/* bad {"count": 2} */}
```ts
import { expect, test } from "vitest";

test("the user has a name", () => {
  expect(typeof user.name).toBe("string");
  expect(typeof user.save).not.toEqual("undefined");
});
```

Not reported:

{/* good */}
```ts
import { expect, test } from "vitest";

test("the user has a name", () => {
  expect(user.name).toBeTypeOf("string");
  expect(user.save).not.toBeTypeOf("undefined");
});
```
