@typescript-eslint/no-extra-non-null-assertion
Disallow a non-null assertion that has no effect
warn by defaultfixable
Also called typescript-eslint/no-extra-non-null-assertion, typescript/no-extra-non-null-assertion
! after an expression tells TypeScript that the value isn't null or undefined. Writing it twice, as in user!!, doesn't add anything. Writing it right before ?. contradicts itself, because user!?.name first claims that user is there and then checks whether it is.
--fix removes the extra !.
This rule is on by default in TypeScript files.
Configure#
On by default as a warning. To turn it off:
bunfig.toml
[lint.rules]
"@typescript-eslint/no-extra-non-null-assertion" = "off" # or "error"Examples#
example.ts2 problems
function nameOf(user?: { name: string }) {
return user!Forbidden extra non-null assertion.!.name;
}
function lengthOf(items?: string[]) {
return items!Forbidden extra non-null assertion.?.length;
}example.tsNo problems
function nameOf(user?: { name: string }) {
return user!.name;
}
function lengthOf(items?: string[]) {
return items?.length;
}