属性 id 未在类型节点中定义 PhpStorm/WebStorm

Property id is not defined in type Node in PhpStorm/WebStorm

Php/WebStorm 在我的 JavaScript 代码中报告了这个奇怪的警告,指出 属性 id 未在 element/node 中定义。事实是,该元素是从父元素中提取的 firstChild 元素。

function hideChildAndUpdateID(title) {
    let parent = document.getElementById('parent');
    let child = parent.firstChild;

    // Hiding the child.
    child.style.display = 'none';
    // Temporary ID so we can find it later.
    child.id = 'child-tmp-id'; // Warning occurs here
}

这是来自 Php/WebStorm

的完整信息
Property id is not defined in type Node less... (Ctrl+F1) 
Inspection info: This inspection reports assignments to undefined properties of explicitly type-annotated variables.

这段代码本身确实有效。 child 元素将其 id 更改为我设置的任何值,但这个警告让我很烦。

我试过使用 child.setAttribute('id', 'child-tmp-id'),但效果不佳。

在处理 firstChild 时是否有约定,或者它只是一个 Php/WebStorm 东西?

节点可以不仅仅是元素,而且不一定具有 id 属性。尝试使用 firstElementChild 而不是 returns 作为元素的第一个子元素:

function hideChildAndUpdateID(title) {
    let parent = document.getElementById('parent');
    let child = parent.firstElementChild;

    // Hiding the child.
    child.style.display = 'none';
    // Temporary ID so we can find it later.
    child.id = 'child-tmp-id'; 
}