typescript - TSLint 说 Expression 总是正确的。(strict-type-predicates) for typeof document !== 'undefined
问题描述
strictNullChecks
启用后,tsconfig.json
我现在收到(TSLint says Expression is always true.
严格类型谓词)typeof document !== 'undefined'
有没有办法纠正这个问题而不用沉默它tslint:disable-next-line: strict-type-predicates
我假设问题是由lib.dom.ts
没有 union的这条线引起的| null
。
declare var document: Document;
我正在做 SSR window
,document
可能不存在。
解决方案
我正在做一个 SSR 项目,我使用“as”运算符来绕过窗口对象的这个 linting 规则,它也适用于文档。
const isBrowser = typeof (window as any) !== 'undefined'
export default isBrowser
推荐阅读
- javascript - 在下一个路由调用中,在一个处理函数中的请求对象上设置的值丢失(未定义)
- python - 如何在 /accounts/register 处修复 UnboundLocalError
- php - 如何根据从另一个外键/ Laravel 6显示的外键显示表格
- python - Jupyter Notebook 500 内部服务器错误属性错误模块“打字”没有属性“AsyncGenerator”
- azure - 如何在 Azure Function 的 EventHubTrigger 连接字符串中为本地环境设置代理
- python - 用于在 python 中循环时匹配字母、数字和特殊章程的正则表达式
- python - 根据 df1 中的值在 df2 中保留一列
- node.js - 尝试在 Docker 中更新 NPM:代码 EISDIR
- python - 使用 SQLite3 TypeError 在 Python 中出现参数错误:+ 的不支持的操作数类型:'sqlite3.Cursor' 和 'str'
- c# - 在以前的属性中选择 null