首页 > 解决方案 > 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的这条线引起的| nulldeclare var document: Document;

我正在做 SSR windowdocument可能不存在。

标签: typescript

解决方案


我正在做一个 SSR 项目,我使用“as”运算符来绕过窗口对象的这个 linting 规则,它也适用于文档。

const isBrowser = typeof (window as any) !== 'undefined'

export default isBrowser

推荐阅读