question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add generics to get/query methods

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:17 (15 by maintainers)

github_iconTop GitHub Comments

7reactions
eps1loncommented, Oct 7, 2020

~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.

3reactions
eps1loncommented, Dec 23, 2020

@timdeschryver A role="button" can be any Element not just HTMLButtonElement. Same applies to most roles.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found