Skip to content
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(expect, @jest/expect-utils): allow isA utility to take a type argument #13355

Merged
merged 2 commits into from
Oct 2, 2022
Merged

feat(expect, @jest/expect-utils): allow isA utility to take a type argument #13355

merged 2 commits into from
Oct 2, 2022

Conversation

mrazauskas
Copy link
Contributor

@mrazauskas mrazauskas commented Oct 2, 2022

Summary

While working on typechecks of expect types, I found out that it would be useful to allow isA utility to take a type argument. This changes turns it into a type guard and could be used for type narrowing.

Test plan

Type tests are added.

@@ -262,10 +262,7 @@ export function isImmutableUnorderedSet(maybeSet: any) {
}

export function isImmutableList(maybeList: any) {
return !!(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and following changes comes from Prettier. CI does not catch them, because of /* eslint-disable */ in this file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just check, I will send a separate PR removing that comment. Seems doable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have purposefully not been running eslint on it to keep the diff from the jasmine equivalent down.

That might not be relevant 6-7 years later

Comment on lines +260 to +261
asymmetricMatch(other: unknown) {
const result = isA<string>('String', other) && other.includes(this.sample);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is just an internal type, user facing external types do not change. The point is that internally other is of type unknown in all the matchers. As an illustration see this test (all matchers have similar tests and that is a problem for typechecking):

https://github.com/facebook/jest/blob/8759d63787b832af1fc9ac0ec2cc57b6f325ba9a/packages/expect/src/__tests__/asymmetricMatchers.test.ts#L321

At first it sounded strange, but reasons are very well explained in #7107

Copy link
Member

@SimenB SimenB left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, very nice!

@SimenB SimenB merged commit d75433d into jestjs:main Oct 2, 2022
@mrazauskas mrazauskas deleted the feat-isA-type-arg branch October 2, 2022 07:53
@github-actions
Copy link

github-actions bot commented Nov 2, 2022

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.
Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 2, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants