首页 > 解决方案 > ESLint 与 TypeScript,如何禁用严格的空检查?

问题描述

ESLint 抱怨我的对象可能为空,即使它周围有一个 if 语句。

     const user: User | null = getUser()

      if (user) { //  if (user !== null) doesn't work either
        await user.updateProfile({
          displayName: this.displayName
        })
      }

有时它不会显示此错误,有时它会显示并且无法摆脱。所以我只想完全禁用这条规则,但我找不到规则名称。

这是 ESLint 中的什么规则名称,或者我还能如何关闭它?

标签: typescripteslint

解决方案


你试过这个吗?

const user: User | null = getUser()

      if (user instanceof User) { //  if (user !== null) doesn't work either
        await user.updateProfile({
          displayName: this.displayName
        })
      }

推荐阅读