Skip to main content

no-non-null-asserted-nullish-coalescing

Disallow non-null assertions in the left operand of a nullish coalescing operator.

Attributes

  • Included in configs
    • ✅ Recommended
    • 🔒 Strict
  • Fixable
    • 🔧 Automated Fixer
    • 🛠 Suggestion Fixer
  • 💭 Requires type information

Rule Details

The nullish coalescing operator is designed to provide a default value when dealing with null or undefined. Using non-null assertions in the left operand of the nullish coalescing operator is redundant.

Examples of code for this rule:

/* eslint @typescript-eslint/no-non-null-asserted-nullish-coalescing: "error" */

foo! ?? bar;
foo.bazz! ?? bar;
foo!.bazz! ?? bar;
foo()! ?? bar;

let x!: string;
x! ?? '';

let x: string;
x = foo();
x! ?? '';

Options

// .eslintrc.json
{
"rules": {
"@typescript-eslint/no-non-null-asserted-nullish-coalescing": "warn"
}
}

This rule is not configurable.

When Not To Use It

If you are not using TypeScript 3.7 (or greater), then you will not need to use this rule, as the nullish coalescing operator is not supported.

Further Reading