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