You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Sometimes we need to extend a TypeScript interface, replacing functions with mocked versions (that are eventually created with vi.fn()).
This works with most functions, but not with generic functions. It used to work up until Vitest 0.15.2, but broke in 0.16.0 and is still broken as-of 0.20.3.
Reproduction
import{MockedFunction}from'vitest'interfaceMyInterface{someFunction<T>(a: number): T}// TS error on the next line from Vitest 0.16.0:// Interface 'MyMockedInterface' incorrectly extends interface 'MyInterface'.// The types returned by 'someFunction(...)' are incompatible between these typesinterfaceMyMockedInterfaceextendsMyInterface{someFunction: MockedFunction<MyInterface['someFunction']>}
Don't use MockedFunction as a type (it's not a return type for vi.fn). Use Mock/MockInstance. We should remove these types from exports I guess. They are for MaybeMocked.
Hm, I got confused, never mind the above. Still, I don't see types working as you expect in 0.15.2. Are you sure you are not updating typescript at the same time as Vitest?
If I downgrade to Vitest 0.15.2 but leave everything else the same, the code in the reproduction works. Note that I have to restart the TypeScript server or else restart Visual Studio Code to refresh everything after downgrading the Vitest version.
This error is because of the mismatch between the return values of both someFunction(). I believe they should have the same return type. I didn't try this code, but have you considered adding the generic to MyMockedInterface's someFunction? It'd look like:
Notice what I'm doing is, instead of just passing MockedFunction, declaring a function type with its return type being the ReturnType of MockedFunction.
Describe the bug
Sometimes we need to extend a TypeScript interface, replacing functions with mocked versions (that are eventually created with
vi.fn()
).This works with most functions, but not with generic functions. It used to work up until Vitest 0.15.2, but broke in 0.16.0 and is still broken as-of 0.20.3.
Reproduction
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: