Add generics to get/query methods
See original GitHub issueDescribe the feature you’d like:
I know the element type I’m querying, so it’d be beneficial to specify it upfront:
screen.queryByText<HTMLButtonElement>('click me'); // -> HTMLButtonElement | null
Type assertions are very error prone, so should be avoided:
screen.queryByText('click me') as HTMLButtonElement; // can still be null!
Suggested implementation:
queryByText<T extends HTMLElement = HTMLElement>(...) => T | null;
Describe alternatives you’ve considered:
You can do already do something similar with for example querySelector
:
document.body.querySelector<HTMLButtonElement>('.my-button');
Teachability, Documentation, Adoption, Migration Strategy:
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:17 (15 by maintainers)
Top Results From Across the Web
Using generics with entities - Stack Overflow
GetQuery method, the PersonEntity is peppered all over code and there are a lot of other entity types that will implement this method...
Read more >Generic Methods - Java™ Tutorials
This Java tutorial describes generics, full screen mode API, and Java certification related resources.
Read more >Generic views - Django REST framework
The view classes can be imported from rest_framework.generics . CreateAPIView. Used for create-only endpoints. Provides a post method handler. Extends: ...
Read more >React Query and TypeScript - TkDodo's blog
Over time, React Query has added more Generics to the useQuery hook (there are now four of them), mainly because more functionality was ......
Read more >SQL Script Generator Using Generics and Reflection
Every time we create a database table(s) or add a new column(s), ... In here, we'll just call the GetQuery() method to return...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
~Definitely going to strongly advocate for type casting here. More explanation in https://twitter.com/SeaRyanC/status/1090998621472940032~
~TL;DR: Using generics in this case is just like type casting without the explicitness of type casting which makes it strictly worse.~
Update: See https://github.com/testing-library/dom-testing-library/issues/615#issuecomment-655411294 for current opinion.
@timdeschryver A
role="button"
can be anyElement
not justHTMLButtonElement
. Same applies to most roles.