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

Add ability for users to supply generic type #258

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

samtsai
Copy link

@samtsai samtsai commented Jul 31, 2023

What:
Add ability to supply generic types similar to Cypress's API

Why:

This allows for further narrowing elements returned by queries in tests instead of just HTMLElement for every element

How:

Update types to include generic <E extends Node = HTMLElement>
Ran npx dslint types locally and validated ExpectType tests still passed

Checklist:

  • Documentation
  • Tests
  • Ready to be merged

@samtsai
Copy link
Author

samtsai commented Aug 1, 2023

Not sure who maintains this project but wanted to get some 👀 on this PR

cc @kentcdodds @MichaelDeBoey @NicholasBoll

@kentcdodds
Copy link
Member

I'm not sure I'm in favor of this change. This is one of those situations where you're letting the compiler lie to you. I would much rather you add a type guard (instanceof) or a cast (as), than hide that away in an abstraction.

@samtsai
Copy link
Author

samtsai commented Aug 1, 2023

I'm not sure I'm in favor of this change. This is one of those situations where you're letting the compiler lie to you. I would much rather you add a type guard (instanceof) or a cast (as), than hide that away in an abstraction.

Yea I'm glad you called it out. I was following the pattern in Cypress itself but actual type guards would be useful. Let me consider this more.

It's also the same way we define it in DOM Testing Library:
https://github.com/testing-library/dom-testing-library/blob/main/types/queries.d.ts#L5

Adding type guards as far as I can tell will require an API change to all the queries, something like this looks appealing: https://effectivetypescript.com/2020/07/27/safe-queryselector/

Still researching but haven't found many great examples in the wild, maybe because it's sort of straightforward?

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

Successfully merging this pull request may close these issues.

2 participants