考虑以下因素:
let container = document.getElementById('container');
let inp = container.querySelector('#my-input-element');
forceDisabled(inp);
/**
* @param {HTMLInputElement} inp
*/
function forceDisabled(inp) {
inp.disabled = true;
}
inp
将是有效的 HTMLInputElement
在运行时。 JetBrains IDE 正确地将其类型视为 Element
。通过“键入”将函数参数合理地设置为 HTMLInputElement
,IDE 会提示:
Argument type
Element
is not assignable to parameter typeHTMLInputElement
IDE确实建议“将参数类型更改为Element
”;但这是完全不合理的,因此函数内部不仅禁用自动完成功能,而且还会提示特定于类的方法的“解决问题”(例如: stepUp()
或 stepDown()
的方法被标记为“未解决”)。
那么,在解决警告的同时,我应该怎么做才能为 IDE 提供适当的“键入提示”(不使用 *
)?!
最佳答案
我明白了! Type-hinting声明中的 inp
应该可以解决该问题:
/** @type {HTMLInputElement} */
let inp = container.querySelector('#my-input-element');
关于JetBrains IDE 上的 JavaScript : assigning the base class type-hinted instance to the parameter type-hinted as a sub-class,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60576562/