Skip to content

vitest/prefer-expect-type-of autofix can produce non-typechecking expectTypeOf(...).toBeFunction() in TS tests #880

@Nick2bad4u

Description

@Nick2bad4u

@vitest/eslint-plugin rule vitest/prefer-expect-type-of suggests/autofixes:

expect(typeof value).toBe("function")

to

expectTypeOf(value).toBeFunction()

But in my setup this can fail TypeScript checking with:

TS2349: This expression is not callable.
Type 'ExpectFunction<unknown>' has no call signatures.

Environment

  • @vitest/eslint-plugin: 1.6.11
  • vitest: 4.1.0
  • TypeScript: 5.9.3
  • Node: 25.8.1
  • OS: Windows 11

repro from repository code

if (firstSuggestion !== undefined) {
  expect(typeof firstSuggestion.fix).toBe("function");
}

When vitest/prefer-expect-type-of is enabled and autofix is applied, this becomes:

expectTypeOf(firstSuggestion.fix).toBeFunction();

Then TypeScript can fail with:

TS2349: This expression is not callable.
Type 'ExpectFunction<unknown>' has no call signatures.

Expected behavior

Autofix should only apply when the resulting assertion is type-safe, or skip autofix if it may break typechecking.

Actual behavior

Autofix/suggestion can lead to TS compile errors for valid runtime assertions.

Would you consider guarding the fixer for cases where expectTypeOf(...).toBeFunction() is not type-safe in the current typing context somehow?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions