-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
NamedNodeMap/getNamedItem is not defined to be potentially "null" #17695
Comments
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. |
@mhegazy I went in to try and fix this issue now and I'm having trouble understanding where exactly you want these fixes to take place? Reading the contributing guide, it appears that all the DOM-related types come from https://github.com/Microsoft/TSJS-lib-generator, however, when I went into that repo to see where it was I could make the fix for this, the only location where there is any mention of Any help pointing me to the correct file to edit would be greatly appreciated. Thanks in advance. |
The change should go into https://github.com/Microsoft/TSJS-lib-generator/blob/master/inputfiles/overridingTypes.json, see https://github.com/Microsoft/TSJS-lib-generator#contribution-guidelines for more details. |
Is it possible to tell TypeScript that null check is not required in this code ? for (let i = 0; i < nodeMap.length; ++i) {
const node = nodeMap.item(i);
// node is never null since i is less than nodeMap.length , but check is required. |
@NN--- |
TypeScript Version: 2.4.2
I've recently faced in issue accessing the
NamedNodeMap
of aNode
- a user managed to insert animg
tag without asrc
attribute. My code accessedattributes.getNamedItem("src")
on the node, and (accidentially) did not check if the resultingattr
exists before accessing its.value
, and the function crashed with a type error.While not performing this check is my personal mistake, I've figured out that
getNamedItem
is not defined to possibly return null: https://github.com/Microsoft/TypeScript/blob/master/lib/lib.es6.d.ts#L14075However, this function will return
null
in case the attributes does not exist:https://developer.mozilla.org/en-US/docs/Web/API/NamedNodeMap/getNamedItem
This should hold true for
getNamedItemNS
as well.Thus, as I've
strict
resp.strictNullChecks
enabled in in mytsconfig.json
, the compiler should raise in error when not checking the result fornull
(see code below).Code
Expected behavior:
The code mentioned above compiles without an error that
src
might benull
, even with the configstrict
resp.strictNullChecks
enabled. It does because the definition mentions that the result ofgetNamedItem
is always anAttr
.Actual behavior:
The code mentioned above should raise a compiler error in case
strict
resp.strictNullChecks
is enabled. The definition should mention that the result ofgetNamedItem
is of typeAttr | null
.The text was updated successfully, but these errors were encountered: