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;
Open in Playgroundfunction foo(bar: number | undefined) {
const bar: number = bar!!!;
}
Open in Playgroundfunction foo(bar?: { n: number }) {
return bar!?.n;
}
Open in Playgroundconst foo: { bar: number } | null = null;
const bar = foo!.bar;
Open in Playgroundfunction foo(bar: number | undefined) {
const bar: number = bar!;
}
Open in Playgroundfunction foo(bar?: { n: number }) {
return bar?.n;
}
Open in PlaygroundOptions
This rule is not configurable.