-
Notifications
You must be signed in to change notification settings - Fork 449
Accept type argument to getElementById #2145
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
base: main
Are you sure you want to change the base?
Conversation
This provides an alternative to the common pattern of `document.getElementById("...") as HTMLWhateverElement` which casts away the possibility of `null`, consequently hiding a potential type error.
Thanks for the PR! This section of the codebase is owned by @saschanaz - if they write a comment saying "LGTM" then it will be merged. |
Might be worth pointing out |
I think the guideline has been that, if you are not actually using the type parameter in the signature then it should not exist, and rather you should cast from the return type. @jakebailey, is that still the case? |
Yes, this effectively a type assertion in disguise. It's not good to have this kind of signature. An explicit assertion is much preferred. |
I understand the risks but I don't think this is a good idea. (If we were to have something like this, it'd at least have to be |
My case for it: Also updated to require that |
I have no idea why these have it, I doubt we would choose to do so if the same decision were made today. It's patently unsafe and hides the type assertion from tools that intend to find unsafety.
|
tbh I have no idea about the tooling landscape here (my version of Vite is called PHP) but to me a
My mistake, the typing I got is from json-with-bigint |
querySelector has it because if you do |
I pinky promise I know what my .html/.php file looks like 😄 |
querySelector has overloads that have nice return types, but then they end with: querySelector<E extends Element = Element>(selectors: string): E | null;
querySelectorAll<E extends Element = Element>(selectors: string): NodeListOf<E>; I'm not sure if that's because it's required to make the overload checks happy, but I don't think so 😦 |
That same argument could apply to all of TS 😄 "I know better than the compiler" is supposed to be casts and |
For me the added With this PR we could migrate to the generics and will get the |
This provides an alternative to the common pattern of
document.getElementById("...") as HTMLWhateverElement
which casts away the possibility ofnull
, consequently hiding a potential type error.