JavaScript/typescript/error:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
第70行: 第70行:
* 在写 <syntaxhighlight lang="javascript" inline>
* 在写 <syntaxhighlight lang="javascript" inline>
document.getElementById('root').className = 'blur';</syntaxhighlight> 的时候提示 <code>Object is possibly null</code>,更改
document.getElementById('root').className = 'blur';</syntaxhighlight> 的时候提示 <code>Object is possibly null</code>,更改
*#<syntaxhighlight lang="javascript">
*# <syntaxhighlight lang="javascript" inline>
(document.getElementById('root') as any).className = 'blur';</syntaxhighlight>
(document.getElementById('root') as Element).className = 'blur';</syntaxhighlight>: 断言
断言
*# <syntaxhighlight lang="javascript" inline>
*#<syntaxhighlight lang="javascript">
document.getElementById('root')!.className = '';</syntaxhighlight>: 感叹号是非null和非undefined的类型断言,非空断言
document.getElementById('root')!.className = '';</syntaxhighlight>
*# <syntaxhighlight lang="javascript" inline>
感叹号是非null和非undefined的类型断言,非空断言
const rootContainer = document.getElementById('root');
if(rootContainer) rootContainer.className = 'blur';</syntaxhighlight>: null的判断
*# <syntaxhighlight lang="javascript" inline>
const rootContainer = document.getElementById('root');
rootContainer && (rootContainer.className = 'blur');</syntaxhighlight>