为什么即使使用IF检查,Flow仍然会抱怨可能为null的值
if(document && document.getElementById("myID") && document.getElementById("myID").offsetWidth){
console.log(document.getElementById("myID").offsetWidth);
}
给出了这个错误
^ property `offsetWidth`. Property cannot be accessed on possibly null value
Flow无法知道第一次调用getElementById
的成功意味着后者也会成功。据他所知,阅读offsetWidth
属性可能会导致getElementById
在下次调用时开始返回null
。
您需要存储该值,例如
const myIdEl = document && document.getElementById("myID");
if(myIdEl && myIdEl.offsetWidth) {
console.log(myIdEl.offsetWidth);
}
这样,myIdEl
在被引用后就无法变为null。
对于FlowType中的HTMLElement(以及像VideoHTMLElement这样的HTMLElement的扩展),我建议使用instanceof
来验证Type并验证它不是null。
另外,我不认为你需要检查document
是否存在,这是在flow(1)*中全局定义的
<HTMLElement>
示例const myIdEl: ?HTMLElement = document.getElementById('myID');
if (myIdEl instanceof HTMLElement) {
// continue
console.log(myIdEl.offsetWidth);
}
<HTMLSelectElement>
示例const selectEl: ?HTMLElement = document.getElementById('someSelectElement');
// Checks correct type (!null !undefined come for free)
if (selectEl instanceof HTMLSelectElement) {
const selectedVal = selectEl.options[selectEl.selectedIndex].value;
}
<HTMLVideoElement>
使用不变量的示例import invariant from 'invariant';
const videoContent = document.getElementById('video-player');
invariant(videoContent instanceof HTMLVideoElement, 'No video element');
// do stuff with video api
videoContent.volume = 0;
videoContent.plause();