62 lines
1.7 KiB
Plaintext
62 lines
1.7 KiB
Plaintext
The usage of non-null assertion in front of assignments or comparators (``++! =++``, ``++! ==++`` or ``++! ===++``) can be too easily confused with a negative comparison.
|
|
|
|
The rule raises an issue when ``++! =++``, ``++! ==++`` and ``++! ===++`` are used with a whitespace between the two operators.
|
|
|
|
== Why is this an issue?
|
|
|
|
The usage of non-null assertion on the left-hand side of an assignment or comparison can be misread and produce unexpected results when one would expect a negative comparator.
|
|
|
|
//=== What is the potential impact?
|
|
|
|
== How to fix it
|
|
|
|
If you really meant to use the non-null assertion on the variable on which the issue is raised, you can simply move the non-null assertion to another expression, or define a new one.
|
|
Otherwise, if an extra whitespace slipped in the middle of a negative comparator, you can simply remove it.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,typescript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
let foo: number | undefined;
|
|
foo = bar! = 12; // Noncompliant; Is that really what's meant?
|
|
if (foo! == bar) { // Noncompliant; Is that really what's meant?
|
|
// ...
|
|
}
|
|
if (foo! === bar) { // Noncompliant; Is that really what's meant?
|
|
// ...
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,typescript,diff-id=1,diff-type=compliant]
|
|
----
|
|
let foo: number | undefined;
|
|
foo = bar = 12;
|
|
if (foo == bar) {
|
|
// ...
|
|
}
|
|
if (foo === bar) {
|
|
// ...
|
|
}
|
|
----
|
|
|
|
//=== How does this work?
|
|
|
|
//=== Pitfalls
|
|
|
|
//=== Going the extra mile
|
|
|
|
|
|
== Resources
|
|
|
|
=== Documentation
|
|
|
|
* TypeScript Documentation - https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-[Non-null Assertion Operator (Postfix ``++!++``)]
|
|
|
|
//=== Articles & blog posts
|
|
//=== Conference presentations
|
|
//=== Standards
|