noExtraNonNullAssertion
Diagnostic Category: lint/suspicious/noExtraNonNullAssertion
Since: v1.0.0
Sources:
Description
Section titled “Description”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 includenullorundefined. Using the operator any more than once on a single value does nothing.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”const bar = foo!!.bar;code-block.ts:1:13 lint/suspicious/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;}code-block.ts:2:10 lint/suspicious/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!))?.();}code-block.ts:2:12 lint/suspicious/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!))?.();
      │               -       
const bar = foo!.bar;
obj?.string!.trim();
function fn(key: string | null) {  const obj = {};  return obj?.[key!];}How to configure
Section titled “How to configure”{  "linter": {    "rules": {      "suspicious": {        "noExtraNonNullAssertion": "error"      }    }  }}