-
Notifications
You must be signed in to change notification settings - Fork 423
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
Replaces several DOM methods with weaker typings and closes microsoft/TypeScript#4689 #885
Conversation
…#4689 The following changes have been made: * All occurences of `getElementById` takes an optional type parameter for manual typing. * All occurences of `getElementsByClassName` takes an optional type parameter for manual typing. * `parentElement` now types to `Element` (See microsoft#4689)
This will break many code parts. |
I was thinking about this, but the spec states IMO, if we continue to let wrong types live in this language, the future will be bleak as new interface may be impossible to implement. Considering |
Yeah, I agree with @HolgerJeromin - I think the move to generics is a good call for usability outside of the common HTML case, but I don't think it should switch to I'll write a longer post in the original TS issue but if this switches to |
@orta Sorry for not getting back to you after a while. I've made the changes you proposed. Your arguments are reasonable, particularly because of the possibility of a 'strict-type' module if ever necessary. However, I did not change the |
Perhaps the parent case can be more complex handled: forreignObjectHTMLElement parent is svgSvgElement. SvgSvgElement can have svgSvgElement or HTMLElement as parent. Or do i miss something? |
Sounds reasonable! I'll make these changes. |
So the changes made so far:
|
inputfiles/idl/DOM.widl
Outdated
@@ -214,7 +214,7 @@ interface Node : EventTarget { | |||
readonly attribute Document? ownerDocument; | |||
Node getRootNode(optional GetRootNodeOptions options = {}); | |||
readonly attribute Node? parentNode; | |||
readonly attribute Element? parentElement; | |||
readonly attribute HTMLElement? parentElement; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a change from upstream?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching this! Happened during a RegExp replace operation..
Can you add similar typings to |
Scope creep. There is already another discussion for that since the new template string types will change the playing field. |
Thanks! |
#970 reverts this PR - so that we can release 4.2 without this. The revert isn't permanent, but it is too breaking to DefinitelyTyped and so it's likely to be very breaking to the rest of the TS/JS ecosystem and Anders argued that this technique is probably not what we want either way microsoft/TypeScript#42067 (comment) It's going to need another look (and maybe we need to figure out a way to be able to determine breakability better inside PRs in this repo, because it takes a few steps to find out right now). |
It is very weird to me that the type parameter would default to I really like that the TypeScript DOM types are a trustworthy reference for how the types in the DOM actually are, often helping uncover and teaching cases that someone may not even know about (and much easier to read and more discoverable than the DOM spec). I think it would be a shame if TypeScript types moved away from modeling the correct types towards only modeling the majority-of-cases types. It is actually very easy to accidentally write code like e.g. document.getElementById(location.hash.slice(1))?.focus() trusting TypeScript that it would give a type error if this was not correct and forgetting that a user-provided ID could very well match an SVG element, which would not have a I understand the challenges with backwards compatibility, but as a user I'd rather update code to add casts to fix type errors. It wouldn't be the first type error in a dom.d.ts upgrade for more correct types 🙂 |
The following changes have been made:
getElementById
take an optional type parameter for manual typing.getElementsByClassName
take an optional type parameter for manual typing.parentElement
now types toElement
(See Node.parentElement should be Element, not HTMLElement TypeScript#4689)