【问题标题】:Assignment of "null" type inferred as "any"“null”类型的赋值推断为“any”
【发布时间】:2021-03-24 04:57:44
【问题描述】:

我发现函数参数缺少类型错误报告:

private _toggle(updates: I_Type | null) {
  const should_toggle = this.thing && !updates.foo

  // ...
}

updates 应该是 I_Typenull 类型之一。但是,!updates.foo 在不使用可选链接时不会显示类型错误。 IE。如果!updates?.foo?. 运算符,则类型错误不应显示。

我不确定我是否误解了什么,或者我的 eslint/tslint 配置有误。 !updates.foo 不应该导致类型错误吗?

更新

根据 VLAZ 的评论,解决方案是将 strict 添加到 tsconfig compilerOptions

【问题讨论】:

  • 您是否启用了strictNullChecks 编译器选项或strict
  • @VLAZ 就是这样 - 非常感谢。在 tsconfig compilerOptions 中添加了strict: true

标签: typescript eslint tslint nrwl-nx


【解决方案1】:

根据 VLAZ 在问题中的评论,解决方案是将 strict 添加到 tsconfig compilerOptions

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 1970-01-01
    • 2020-09-28
    相关资源
    最近更新 更多