-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Element.closest should return specific types for tag selectors #17550
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
Comments
Would you look at that, a hack-job cut and paste worked on the first try! I'm running a vanilla-JS project in VSCode but when I put this in my declare global {
interface Element extends Node, GlobalEventHandlers, ElementTraversal, NodeSelector, ChildNode, ParentNode {
closest<K extends keyof ElementTagNameMap>(selector: K): ElementTagNameMap[K] | null;
}
} I copied that verbatim from |
Out of curiosity I searched |
@mhegazy I believe that was just a typo. Please re-open this issue. Code: let a = document.querySelector("a");
if (!a) throw new Error();
let div = a.closest("div"); Expected behavior:
Actual behavior:
|
PRs welcomed. You can find more information about contributing lib.d.ts fixes at https://github.com/Microsoft/TypeScript/blob/master/CONTRIBUTING.md#contributing-libdts-fixes. |
Yes, sorry, typo -- I had meant |
Seems like it is missing the following line. I can submit a PR if this seems correct. @mhegazy
|
TypeScript Version: 2.4.0
Code
Expected behavior:
a
is anHTMLAnchorElement
, anddiv
is anHTMLDivElement
.Actual behavior:
el2
is actually anElement
becauseElement.closest
always returns an Element. This was fixed forquerySelector
in #8114 and the same behavior should be applied toclosest
.The text was updated successfully, but these errors were encountered: