deno.com

禁止多余的非空断言

注意:此规则是 recommended 规则集的一部分。
deno.json 中启用完整集合
{
  "lint": {
    "rules": {
      "tags": ["recommended"]
    }
  }
}
使用 Deno CLI 启用完整集合
deno lint --rules-tags=recommended
通过将其添加到 deno.json 中的 includeexclude 数组,此规则可以被显式地包含或排除在当前标签中的规则之外。
{
  "lint": {
    "rules": {
      "include": ["no-extra-non-null-assertion"],
      "exclude": ["no-extra-non-null-assertion"]
    }
  }
}

禁止不必要的非空断言。

非空断言使用 ! 指定,它告诉编译器您知道此值不为空。连续多次指定此操作符,或将其与可选链操作符(?)结合使用,是令人困惑且不必要的。

无效示例

const foo: { str: string } | null = null;
const bar = foo!!.str;

function myFunc(bar: undefined | string) {
  return bar!!;
}
function anotherFunc(bar?: { str: string }) {
  return bar!?.str;
}

有效示例

const foo: { str: string } | null = null;
const bar = foo!.str;

function myFunc(bar: undefined | string) {
  return bar!;
}
function anotherFunc(bar?: { str: string }) {
  return bar?.str;
}

您找到所需内容了吗?

隐私政策