Skip to content

Latest commit

 

History

History
54 lines (36 loc) · 983 Bytes

non-nullable-type-assertion-style.md

File metadata and controls

54 lines (36 loc) · 983 Bytes

non-nullable-type-assertion-style

Prefers a non-null assertion over explicit type cast when possible.

This rule detects when an as cast is doing the same job as a ! would, and suggests fixing the code to be an !.

Rule Details

Examples of code for this rule:

❌ Incorrect

const maybe = Math.random() > 0.5 ? '' : undefined;

const definitely = maybe as string;
const alsoDefinitely = <string>maybe;

✅ Correct

const maybe = Math.random() > 0.5 ? '' : undefined;

const definitely = maybe!;
const alsoDefinitely = maybe!;

Options

// .eslintrc.json
{
  "rules": {
    "@typescript-eslint/non-nullable-type-assertion-style": "warn"
  }
}

This rule is not configurable.

When Not To Use It

If you don't mind having unnecessarily verbose type casts, you can avoid this rule.

Attributes

  • Configs:
    • ✅ Recommended
    • 🔒 Strict
  • 🔧 Fixable
  • 💭 Requires type information