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

Fix typing for hasProperty assertion #782

Merged
merged 1 commit into from
Aug 13, 2020

Conversation

wuron
Copy link
Contributor

@wuron wuron commented Aug 9, 2020

Fixes #781

API.md Outdated Show resolved Hide resolved
@wuron
Copy link
Contributor Author

wuron commented Aug 11, 2020

So what do we need to merge this?

* @param {string?} message
*
* @example
* assert.dom('input.password-input').hasProperty('type', 'password');
*
* @see {@link #doesNotHaveProperty}
*/
hasProperty(name: string, value: string | RegExp, message?: string): DOMAssertions {
hasProperty(name: string, value: unknown, message?: string): DOMAssertions {
Copy link
Collaborator

Choose a reason for hiding this comment

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

why are you using unknown here instead of RegExp | any as specified above?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RegExp | any doesn’t make any sense in typescript, it will still evaluate as any. And unknown is safer then any because it doesn’t imply any knowledge about type.

As for JSDoc annotation there’s no unknown type, so any is the closest alternative. And it’s still helpful to mention RegExp because it has special treatment (match instead of just equality check).

Choose a reason for hiding this comment

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

@wuron is correct: types in TS are sets, and since any is already a superset of everything else, having T | any, for any type, is always exactly identical to just having any.

Choose a reason for hiding this comment

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

Addendum: you can actually use unknown in JSDoc and most tools will just show "unknown" there, and anything TS-aware will actually correctly interpret it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

you can actually use unknown in JSDoc and most tools will just show "unknown" there, and anything TS-aware will actually correctly interpret it.

If that’s preferred I can set it to RegExp | unknown in JSDoc. But does it really change anything? JSDoc definitions are only used as documentation: to generate API.md file, or to hint types for non-TS environments. In both cases we want to communicate that this method accepts any value, hence any works just fine.

In TS definitions we shouldn’t use any, because it’s compatible with all types and allows us to do something like this without any warnings:

function hasProperty(param: any) {
  param.callRandomMethod();
  const a: boolean = param;
  const b: string = param;
}

Parameter with type unknown is much safer, and it still allows to pass any value.

Copy link
Collaborator

@Turbo87 Turbo87 left a comment

Choose a reason for hiding this comment

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

alright, thanks for the explanation. LGTM :)

@Turbo87 Turbo87 changed the title Fix typing for hasProperty assertion. Fix typing for hasProperty assertion Aug 13, 2020
@Turbo87 Turbo87 merged commit 83c9cfd into mainmatter:master Aug 13, 2020
@chriskrycho
Copy link

@Turbo87 would you mind cutting a release which includes this? 🙏 Would love to take advantage of it in a small library I'm open-sourcing!

@Turbo87
Copy link
Collaborator

Turbo87 commented Sep 16, 2020

v1.5.0 should be out in a couple of minutes once CI is done :)

@chriskrycho
Copy link

Thank you! Really appreciate it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect typing for hasProperty assertion
3 participants