PhpStorm 的类型提示 jQuery 方法

问题描述

我遇到了与 this question 相同的问题,但我使用的是 JavaScript,而不是 TypeScript,因此那里的解决方案不适用。

使用以下代码

$("input").each(function(i,el) {
    console.log(el.value);
});

我收到关于 el.value 的投诉:

未解析的变量值

这是因为 jQuery 类型将 el 提示HTMLElement,但 value 属性仅存在于某些子类中,例如 HTMLInputElement

我发现消除此警告的唯一方法是这样的,我不会因为一个小警告而疯狂(并且在某些情况下它会产生另一个关于“冗余变量”的警告) :

$("input").each(function(i,el) {
    /** @type {HTMLInputElement} */
    const input = el;
    console.log(input.value);
});

有没有更简洁的方法来处理这个问题?我确实尝试过这样的内联类型定义,但没有任何运气:

$("input").each(function(i,/** @type {HTMLInputElement} */el) {
    console.log(el.value);
});

以下是它们在 IDE 中的外观:

screenshot of problem

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)