-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
querySelector return type could be more specific for compound selectors #29037
Comments
Ref #21044 which'd enable us to support this. |
Could this also work for complex selectors and lists of them? document.querySelector('.wrapper div.box');
document.querySelector('.wrapper div.box, .sidebar div.alert'); |
@bfred-it I could see it working for a single complex selector, since it selects only one element (or an array of the same type of element). But I don’t think it would work for selector lists, since there’s a chance more than one type of element could be selected. To follow your example, what would |
Yes, actually that's what I meant to write, two different elements. If that's not possible, at least support for a homogeneous list of selectors would still be useful: |
This is now possible with #40336. |
Wow, I never thought this was going to be possible. @g-plane will you open a PR to merge it into lib.dom.ts? |
I'd like to hear opinions from TypeScript team before doing it. |
I made a playground to play about with it (interesting work @g-plane! ) - I'll bring it up in a design meeting (maybe next week will have some time), my bet is that we need to figure out the perf trade-offs for that feature. I'd use it a lot though, so you have me on your side at least!
|
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@orta There's something wrong with the playground you made. For Line 56 and Line 57: - const nestedDiv = document.querySelector('.wrapper div.box');
+ const nestedDiv = querySelector('.wrapper div.box');
- const subNestedDiv = document.querySelectorAll('.wrapper div.box, .sidebar div.alert');
+ const subNestedDiv = querySelectorAll('.wrapper div.box, .sidebar div.alert'); Then, it works. |
Would it be possible to open a PR with this change from the playground ? Having improved support for |
For anyone who want this feature now, I've released an npm package for you. Here is the repository: https://github.com/g-plane/typed-query-selector . Feel free to use and star it. |
Search Terms
querySelector, return, type, selector
Suggestion
This issue closely follows #8114, which applies only to type selectors ("single-element selectors"). Related to #12568.
The return type of ParentNode#querySelector is
Element
by default, but when the string argument matches exactly a lower-case element name, the return type is the interface of that element.For example,
.querySelector('#hero.wide')
returns anElement
type, but.querySelector('img')
returns anHTMLImageElement
type.This helpful behavior fails beyond simple type selectors (a selector containing only an element name). When the argument becomes a compound selector, such as
.querySelector('img#hero.wide')
, the return type is the more genericElement
. This is unhelpful when the element name,img
, remains in the selector.My suggestion is to improve parsing of the string argument, so that when it is a compound selector that contains a type selector, the return type can still be a specific interface. Obviously, this would not apply to selectors not containing a type selector, e.g. there is no way to know for sure that
.querySelector('#hero.wide')
is indeed anHTMLImageElement
.Use Cases
Summary: It would be nice for TS to infer that
img#hero.wide
selects anHTMLImageElement
, based on the tag nameimg
in the selector. This would eliminate the need to assert the type manually.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: