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
[@types/jquery] Return type for .get() is HTMLElement, should be HTMLElement | undefined #52052
Comments
|
Please. Can you try? |
|
@martin-badin Hi Martin, I'm sorry, I don't understand how casting |
|
I'm sorry, I misunderstood. By the documentation is the return type correct https://api.jquery.com/get/. I know what you have in your mind but I think that your example/code is wroten wrong. Your code should be like this because |
|
Yes, to prevent errors where there's no element with ID Or equally: I'm filing this issue because |
|
@martin-badin FYI, documentation you linked to states "If the value of |
|
@ethanredmond2 - I agree with you - it's exactly what the docs state. I've made a pull request for this change: #56118 |
#56118) Co-authored-by: Piotr Błażejewicz <peterblazejewicz@users.noreply.github.com>
|
I think we can close this issue as solved. |
|
fixed in #56118 |
JQuery's
.get()method reports that it always returns Element, however in practice we can see that this is not always true, for example where there is no element with the IDasdf:To fix this, you could change the return type of
.get()toHTMLElement | undefined. This may cause new warnings when other projects update their version of @types/jquery, however I believe it'll prevent possible JavaScript errors at runtime.TypeScript's
strictNullCheckssetting is specifically designed to warn about this kind of problem, for example,document.getElementById()returnsHTMLElement | undefined. This encourages safer coding practices like optional chaining, e.g.document.getElementById('asdf')?.focus()@leonard-thieu @borisyankov @choffmeister @Diullei @tasoili @jasons-novaleaf @seanski @Guuz @ksummerlin @basarat @nwolverson
@derekcicerone @AndrewGaspar @seikichi @benjaminjackman @s093294 @JoshStrobl @johnnyreilly @DickvdBrink @King2500 @terrymun @martin-badin
(I'm using TypeScript 3.9.9 and @types/jquery 3.5.5)
The text was updated successfully, but these errors were encountered: