Open danielelkington opened 2 years ago
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:
interface MyInterface {
someFunction<T>(a: number): T;
}
interface MyMockedInterface extends MyInterface {
someFunction: <T>(...params: Parameters<MyInterface['someFunction']>) => ReturnType<MockedFunction<() => T>>;
}
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