Skip to content

Conversation

@falecci
Copy link

@falecci falecci commented Oct 18, 2025

When running the workshop app, in the 04. Globals / 01. Global Methods exercise, there is a bonus point that talks about extending the global types to access console.log.mock.calls which otherwise would throw a type error (http://localhost:5639/exercise/04/01/solution#bonus-type-safe-global-spies)

The proposed code to do so is:

import { MockInstance } from 'vitest'

declare namespace console {
  var log: Console['log'] &
    MockInstance<Parameters<Console['log']>, ReturnType<Console['log']>>
}

But I don't think that's valid anymore since my TS compiler was complaining. The actual usage seems to be much simpler:

import { MockInstance } from "vitest";

declare namespace console {
  var log: Console["log"] &
    MockInstance<Console["log"]>;
}

While searching for docs, it seems this change has been introduced in Vitest 2.0: https://vitest.dev/guide/migration#simplified-generic-types-of-mock-functions-e-g-vi-fn-t-mock-t

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant