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

querySelector return type could be more specific for single-element selectors #8114

Closed
jelbourn opened this issue Apr 16, 2016 · 5 comments · Fixed by microsoft/TypeScript-DOM-lib-generator#102
Labels
Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript

Comments

@jelbourn
Copy link

The return types for querySelector and querySelectorAll (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

interface NodeSelector {
    querySelector(selectors: string): Element;
    querySelectorAll(selectors: string): NodeListOf<Element>;
}

But it could be expanded in the style of getElementsByTagName:

interface NodeSelector {
    querySelector(selectors: "a"): HTMLAnchorElement;
    querySelector(selectors: "abbr"): HTMLPhraseElement;
    ...
    querySelector(selectors: string): Element;
}

It's a minor convenience, but does eliminate the occasional typecast.

@DanielRosenwasser DanielRosenwasser added the Suggestion An idea for TypeScript label Apr 17, 2016
@DanielRosenwasser
Copy link
Member

I don't think this is totally unreasonable. @mhegazy @zhengbli any thoughts on this?

@mhegazy mhegazy added this to the Community milestone Apr 18, 2016
@mhegazy mhegazy added the Help Wanted You can do this label Apr 18, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Apr 18, 2016

PRs welcomed. here is some instructions on submitting lib.d.ts PRs: https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes

@thw0rted
Copy link

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?

@kitsonk
Copy link
Contributor

kitsonk commented Jul 31, 2017

Pattern matching of overloads is #165 and it being applied to querySelector() would be different than this closed issue, which has been resolved.

@thw0rted
Copy link

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.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue Help Wanted You can do this Suggestion An idea for TypeScript
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants