Repository navigation
querySelector return type could be more specific for single-element selectors #8114
Description
Activity
- addedSuggestionAn idea for TypeScriptAn idea for TypeScript
on Apr 17, 2016 DanielRosenwasser commented
on Apr 17, 2016 MemberMore actionsI don't think this is totally unreasonable. Mohamed Hegazy (@mhegazy) Zhengbo Li (@zhengbli) any thoughts on this?
PRs welcomed. here is some instructions on submitting lib.d.ts PRs: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes
- addedFixedA PR has been merged for this issueA PR has been merged for this issueand removed
on Jun 17, 2016 I'm a vanilla Javascript user but I benefit from this in VS Intellisense. So, I don't know TS well enough to answer my question: would it be possible to implement this with regex or partial-string matching, such that
querySelector("a.someClass")also returns a more specific class (i.e.HTMLAnchorElement)? It's the same operating theory, right? I don't think it's possible for querySelector to return a non-Anchor for the selector example I gave, is it?Pattern matching of overloads is #165 and it being applied to
querySelector()would be different than this closed issue, which has been resolved.OK, I think that fills in the blanks of the Typescript syntax I was missing. I asked on StackOverflow about good ways to handle this in the short term, and I'm happy enough with the answer, but returning a more specific type (eventually) would make for better code.
- locked and limited conversation to collaborators
on Jun 19, 2018
The return types for
querySelectorandquerySelectorAll(in the lib.es6.d.ts) could provide more specific return type information when the selector a single element selector for a known element type.I.e., right now the types are
But it could be expanded in the style of
getElementsByTagName:It's a minor convenience, but does eliminate the occasional typecast.