Lint Rules

noExtraNonNullAssertion (since v11.0.0)

Prevents the wrong usage of the non-null assertion operator (!) in TypeScript files.

The ! non-null assertion operator in TypeScript is used to assert that a value’s type does not include null or undefined. Using the operator any more than once on a single value does nothing.

Source: https://typescript-eslint.io/rules/no-extra-non-null-assertion

Examples

Invalid

const bar = foo!!.bar;
nursery/noExtraNonNullAssertion.js:1:13 lint/nursery/noExtraNonNullAssertion  FIXABLE  ━━━━━━━━━━━━━

   Forbidden extra non-null assertion.
  
  > 1 │ const bar = foo!!.bar;
               ^^^^
    2 │ 
  
   Safe fix: Remove extra non-null assertion.
  
    1 │ const·bar·=·foo!!.bar;
                  -     
function fn(bar?: { n: number }) {
  return bar!?.n;
}
nursery/noExtraNonNullAssertion.js:2:10 lint/nursery/noExtraNonNullAssertion  FIXABLE  ━━━━━━━━━━━━━

   Forbidden extra non-null assertion.
  
    1 │ function fn(bar?: { n: number }) {
  > 2 │   return bar!?.n;
            ^^^^
    3 │ }
    4 │ 
  
   Safe fix: Remove extra non-null assertion.
  
    2 │ ··return·bar!?.n;
              -    
function fn(bar?: { n: number }) {
  return ((bar!))?.();
}
nursery/noExtraNonNullAssertion.js:2:12 lint/nursery/noExtraNonNullAssertion  FIXABLE  ━━━━━━━━━━━━━

   Forbidden extra non-null assertion.
  
    1 │ function fn(bar?: { n: number }) {
  > 2 │   return ((bar!))?.();
              ^^^^
    3 │ }
    4 │ 
  
   Safe fix: Remove extra non-null assertion.
  
    2 │ ··return·((bar!))?.();
                -       

Valid

const bar = foo!.bar;

obj?.string!.trim();

function fn(key: string | null) {
  const obj = {};
  return obj?.[key!];
}