no-extra-non-null-assertion
Disallow extra non-null assertions.
✅
Extending "plugin:@typescript-eslint/recommended"
in an ESLint configuration enables this rule.
🛠
Some problems reported by this rule are automatically fixable by the --fix
ESLint command line option.
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.
.eslintrc.cjs
module.exports = {
"rules": {
"@typescript-eslint/no-extra-non-null-assertion": "error"
}
};
Examples
- ❌ Incorrect
- ✅ Correct
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;
}
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;
}
Options
This rule is not configurable.