-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
feat(@jest/expect): Expose type of actual to Matchers #13848
Conversation
Matchers isn't as typed as some users would like (see jestjs#13334, jestjs#13812). For users who want to customize it by extending the `Matchers` interface, it's useful to have access to the type of `actual` (the argument of `expect`) so you can do, say, ```ts interface Matchers<R, T> { toTypedEqual(expected: T): R } ``` This commit exposes it. The first-party matchers still have the same types as before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I think this is useful for custom matchers in general. For example, the inferred type of actual
is used in snapshot matchers through @jest/expect
. I think user of expect
should be allowed to have similar functionality for a custom matcher.
Also because this was allowed before #12404 and still works with @types/jest
. That's why I see this PR as a fix of a regression.
Could you add:
type N = Matchers<void, string>;
Under this line:
Also please add a type test with your example of toTypedEqual
here (simply to prevent regression):
CHANGELOG.md
Outdated
@@ -2,6 +2,8 @@ | |||
|
|||
### Features | |||
|
|||
- `[@jest/expect]` provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions (#TBD) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can go under fixes as well.
- `[@jest/expect]` provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions (#TBD) | |
- `[expect, @jest/expect]` Provide type of `actual` as a generic argument to `Matchers` to allow better-typed extensions ([#13848](https://github.com/facebook/jest/pull/13848)) |
Thanks for all the helpful comments! I think I've addressed all of them. A couple notes:
|
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
packages/expect/src/types.ts
Outdated
// @ts-expect-error unused variable T (can't use _T since users redeclare Matchers) | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export interface Matchers<R extends void | Promise<void>, T = unknown> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What about this:
// @ts-expect-error unused variable T (can't use _T since users redeclare Matchers) | |
// eslint-disable-next-line @typescript-eslint/no-unused-vars | |
export interface Matchers<R extends void | Promise<void>, T = unknown> { | |
export interface Matchers<R extends void | Promise<void>, T = unknown> { | |
/** @internal */ | |
_doKeepT(expect: T): R; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could have some longer comment, but seems to be working. Or?
stripInternal
documentation: https://www.typescriptlang.org/tsconfig#stripInternal
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, great! I thought of adding a fake method but didn't realize I could hide it like that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Two last details from my side. The rest looks good.
Type tests are passing, because emitted types keep T
as expected:
It is marked as unused in this screenshot of .d.ts
file. So I went checking if that is not a problem for a user having "skipLibCheck": true
in a tsconfig.json
. TypeScript did not complain. Seems to be fine.
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
Thanks for checking, I looked at the |
Co-authored-by: Tom Mrazauskas <tom@mrazauskas.de>
Yes, |
|
Right, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks!
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Summary
Matchers isn't as typed as some users would like (see #13334, #13812). For users who want to customize it by extending the
Matchers
interface, it's useful to have access to the type ofactual
(the argument ofexpect
) so you can do, say,This commit exposes it. The first-party matchers still have the same types as before.
Test Plan
typecheck