JavaScript

JavaScript

Made by DeepSource

Unnecessary non-null assertion JS-0324

Anti-pattern
Minor
Autofix

Multiple non-null assertion operators (!s) are redundant, and may confuse the reader. Moreover, foo?.bar should always be preferred over foo!?.bar.

Bad Practice

const foo: { bar: number } | null = null;
const bar = foo!!!.bar;

function foo(bar: number | undefined) {
  const bar: number = bar!!!;
}

function foo(bar?: { n: number }) {
  return bar!?.n;
}

Recommended

const foo: { bar: number } | null = null;
const bar = foo!.bar;

function foo(bar: number | undefined) {
  const bar: number = bar!;
}

function foo(bar?: { n: number }) {
  return bar?.n;
}