-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The simplified MockInstance<T>
type does not work with function overloads
#6085
Comments
Mock<T>
type does not work with function overloadsMockInstance<T>
type does not work with function overloads
Thanks for the report. I haven't thought about this case. Similar code on Jest has no type errors, but it looks like it still has some limitations with function overloads. test("overload", () => {
// they have dedicated `Spied` type to align `spyOn` return type.
// so assignment works fine
let spy: jest.Spied<Element["scrollTo"]>
spy = jest.spyOn(Element.prototype, "scrollTo");
// but only 2nd overload argument types
spy.mock.calls satisfies [x: number, y: number][];
// for `Mock`, probably Vitest and Jest work same.
let mockFn: jest.Mock<Element["scrollTo"]>;
mockFn = jest.fn(Element.prototype.scrollTo);
mockFn(0, 1); // 2nd overload ok
// @ts-expect-error
mockFn({ top: 0 }); // 1st overload error
}) Probably this comes down to type OverloadFn = {
(x: number, y: number): void;
(someOptions?: { top?: number }): void;
}
// this becomes `[x: number, y: number]`
type OverloadArgs = Parameters<Element["scrollTo"]> |
That's true. Like I provided above, even in |
Seems like a hard limitation there, emmm,..... 🤦♀️ See this also: microsoft/TypeScript#32164 (comment) |
But I think at least we should try to find a way to avoid the type error there, and maybe document the TS limitation on function overloads |
This type mismatch is probably because we split types here vitest/packages/spy/src/index.ts Lines 401 to 408 in 99a12ae
|
Describe the bug
It won't work with function overloads, refer to the reproduction.
Reproduction
type error in
v2
https://stackblitz.com/edit/vitest-dev-vitest-hlq3zj?file=test%2Fbasic.test.ts
No type error in
v1
, but seems the other overload signature is being ignoredhttps://stackblitz.com/edit/vitest-dev-vitest-cayu3q?file=test%2Fbasic.test.ts
System Info
Used Package Manager
npm
Validations
The text was updated successfully, but these errors were encountered: