Skip to content

Latest commit

 

History

History
67 lines (49 loc) · 1.47 KB

no-confusing-non-null-assertion.md

File metadata and controls

67 lines (49 loc) · 1.47 KB
description
Disallow non-null assertion in locations that may be confusing.

🛑 This file is source code, not the primary documentation location! 🛑

See https://typescript-eslint.io/rules/no-confusing-non-null-assertion for documentation.

Rule Details

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

a! == b; // a non-null assertions(`!`) and an equals test(`==`)
a !== b; // not equals test(`!==`)
a! === b; // a non-null assertions(`!`) and an triple equals test(`===`)

❌ Incorrect

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

const foo: Foo = getFoo();
const isEqualsBar = foo.bar! == 'hello';
const isEqualsNum = 1 + foo.num! == 2;

✅ Correct

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

const foo: Foo = getFoo();
const isEqualsBar = foo.bar == 'hello';
const isEqualsNum = (1 + foo.num!) == 2;

Options

// .eslintrc.json
{
  "rules": {
    "@typescript-eslint/no-confusing-non-null-assertion": "warn"
  }
}

This rule is not configurable.

When Not To Use It

If you don't care about this confusion, then you will not need this rule.

Further Reading