我有一个类型:
type tSelectProtected = {
handleSelector?: string,
data?: tSelectDataItem[],
wrapperEle?: HTMLElement,
inputEle?: HTMLElement,
listEle?: HTMLElement,
resultEle?: HTMLElement,
maxVisibleListItems?: number
}
我声明了一个全局模块变量:
var $protected : tSelectProtected = {};
我在function1()范围内分配适当的值:
$protected.listEle = document.createElement('DIV');
稍后在function2()作用域中,我调用:
$protected.listEle.classList.add('visible');
我得到TypeScript错误:
error TS2533: Object is possibly 'null' or 'undefined'
我知道我可以使用if ($protected. listele) {$protected. listele进行显式检查。listEle}使编译器平静下来,但这似乎对于大多数非平凡的情况非常不方便。
在不禁用TS编译器检查的情况下如何处理这种情况?