org.sonar.l10n.javascript.rules.javascript.S2966.html Maven / Gradle / Ivy
Why is this an issue?
The point of declaring an optional property or parameter is to make explicit the fact that it might contain no valid value, i.e. null
or undefined
. Using a non-null assertion (the !.
operator) will lead to a runtime error if the optional does contain
null
or undefined
. Even if the value is tested first, it’s still considered a bad practice to use a non-null assertion.
Noncompliant code example
function doTheThing(foo?: Foo) {
let s = foo!.bar; // Noncompliant
}
Compliant solution
function doTheThing(foo?: Foo) {
if (foo) {
let s = foo.bar;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy