@typescript-eslint/no-confusing-non-null-assertion
Disallow a non-null assertion ! right before =, ==, ===, in or instanceof
has suggestions
Also called typescript-eslint/no-confusing-non-null-assertion, typescript/no-confusing-non-null-assertion
user! == other asserts that user isn't null and then tests for equality. It reads like user !== other, which means the opposite. In the same way, count! = 1 looks like count != 1, and key! in cache looks like !(key in cache).
The report comes with a suggestion to remove the !, or to put the left side in parentheses.
Configure#
Off by default. To turn it on:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-confusing-non-null-assertion" = "error" # or "warn"Examples#
example.ts3 problems
declare let user: string | undefined;
declare const cache: Record<string, number>;
const isSame = user! == "ada"Confusing combination of non-null assertion and equality test like a! == b, which looks very similar to a !== b.;
user! = "grace"Confusing combination of non-null assertion and assignment like a! = b, which looks very similar to a != b.;
const isCached = user! in cacheConfusing combination of non-null assertion and in operator like a! in b, which might be misinterpreted as !(a in b).;example.tsNo problems
declare let user: string | undefined;
declare const cache: Record<string, number>;
const isSame = user == "ada";
user = "grace";
const isCached = (user!) in cache;
const isOther = "ada" == user!;