Javascript 在 JetBrains IDE 上:将基础 class 类型提示实例分配给参数类型提示作为子 class
Javascript on JetBrains IDE: assigning the base class type-hinted instance to the parameter type-hinted as a sub-class
考虑以下几点:
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
at runtime. The JetBrains IDE correctly considers its type as being an Element
。由"typing"函数参数合理为HTMLInputElement
,IDE抱怨:
Argument type Element
is not assignable to parameter type HTMLInputElement
IDE建议"change the parameter type to Element
";但这将是完全不合理的,因此函数内部不仅禁用了自动完成功能,而且还在 "resolving issues" 上抱怨 class-specific 方法 (例如:方法 stepUp()
or stepDown()
将被标记为 "unresolved").
那么,我应该怎么做才能让 适当 "typing hints" 到 IDE (不使用 *
) 而有警告解决了?!
我想通了! Type-hinting 声明中的 inp
应该可以解决问题:
/** @type {HTMLInputElement} */
let inp = container.querySelector('#my-input-element');
考虑以下几点:
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
at runtime. The JetBrains IDE correctly considers its type as being an Element
。由"typing"函数参数合理为HTMLInputElement
,IDE抱怨:
Argument type
Element
is not assignable to parameter typeHTMLInputElement
IDE建议"change the parameter type to Element
";但这将是完全不合理的,因此函数内部不仅禁用了自动完成功能,而且还在 "resolving issues" 上抱怨 class-specific 方法 (例如:方法 stepUp()
or stepDown()
将被标记为 "unresolved").
那么,我应该怎么做才能让 适当 "typing hints" 到 IDE (不使用 *
) 而有警告解决了?!
我想通了! Type-hinting 声明中的 inp
应该可以解决问题:
/** @type {HTMLInputElement} */
let inp = container.querySelector('#my-input-element');