no-extra-non-null-assertion
NOTE: this rule is part of the
recommended
rule set.Enable full set in
deno.json
:{ "lint": { "rules": { "tags": ["recommended"] } } }
Enable full set using the Deno CLI:
deno lint --rules-tags=recommended
This rule can be explictly included to or excluded from the rules present in the current tag by adding it to the
include
or exclude
array in deno.json
:{ "lint": { "rules": { "include": ["no-extra-non-null-assertion"], "exclude": ["no-extra-non-null-assertion"] } } }
禁止不必要的非空断言。
非空断言使用 !
指示编译器你知道这个值不为 null。连续使用该操作符超过一次,或者与可选链操作符(?
)结合使用是令人困惑且不必要的。
无效示例:
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;
}