【问题标题】:How to tell to JetBrains IDE that this javascript variable changed type to another?如何告诉 JetBrains IDE 这个 javascript 变量将类型更改为另一个?
【发布时间】:2021-02-05 15:27:10
【问题描述】:

Web API 具有扩展Node 接口的Element 接口。 如何告诉 JetBrains IDE 这个特定的 Node 是 Element?

希望下面的代码能解释一切。

/**
* @param {Node} node 
* @return {string}
*/
function extractor(node) {
   let text;
   if (node.nodeType === Node.ELEMENT_NODE) {
       // On the next line IDE shows a warning,
       // that innerHTML property is not defined for Node.
       // How to tell IDE that node variable became and Element here?
       text = node.innerHTML;
   } else {
       text = node.textContent;
   }
   return text;
}

我正在使用 PyCharm Professional,但我认为这并不重要。

【问题讨论】:

    标签: javascript intellij-idea jsdoc


    【解决方案1】:

    你不能。

    如果你愿意,你可以使用 Typescript。

    text = (node as Element).innerHTML;
    

    但是在javascript中,没有严格的类型,你只能试试:

    /**
    * @param {Node | Element} node 
    * @return {string}
    */
    

    【讨论】:

    • 不可能。但您可以在 JetBrains IDE 中禁用该警告。
    • 就我而言,抑制此警告更容易。但感谢您提到 Typescript。看起来很有趣。
    猜你喜欢
    • 1970-01-01
    • 2016-11-27
    • 2015-07-11
    • 2017-06-16
    • 2011-09-05
    • 2013-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多