Skip to content

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

Merged
SimenB merged 2 commits intojestjs:mainfrom
mrazauskas:feat-isA-type-arg
Oct 2, 2022
Merged

feat(expect, @jest/expect-utils): allow isA utility to take a type argument#13355
SimenB merged 2 commits intojestjs:mainfrom
mrazauskas:feat-isA-type-arg

Conversation

@mrazauskas
Copy link
Copy Markdown
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.

}

export function isImmutableList(maybeList: any) {
return !!(
Copy link
Copy Markdown
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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