目录

@typescript-eslint/no-confusing-non-null-assertion

Disallow non-null assertion in locations that may be confusing.

Using a non-null assertion (!) next to an assign or equals check creates code that is confusing as it looks similar to a not equals check.

Rule Configuration

// code-linter.json5
{
  "rules": {
    "@typescript-eslint/no-confusing-non-null-assertion": "error"
  }
}

Options

This rule is not configurable.

Example of Correct Code

interface Foo {
  bar?: string;
  num?: number;
}

function getFoo(): Foo {
  return {
    bar: 'bar',
    num: Number.MAX_VALUE
  };
}
const foo: Foo = getFoo();
export const isEqualsBar = foo.bar === 'hello';
const num = 2;
export const isEqualsNum = num + (foo.num!) === num;

Example of Incorrect Code

interface Foo {
  bar?: string;
  num?: number;
}

function getFoo(): Foo {
  return {
    bar: 'bar',
    num: Number.MAX_VALUE
  };
}
const foo: Foo = getFoo();
// Confusing. It looks similar to a not equals check.
export const isEqualsBar = foo.bar! === 'hello';
// Confusing. It looks similar to a not equals check.
const num = 2;
export const isEqualsNum = num + foo.num! === num;

Rule Set

plugin:@typescript-eslint/all

For details about how to configure Code Linter check rules, see Code Linter.