@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";
user! = "grace";
const isCached = user! in cache;
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!;