nonNullAssertionPlacement
Reports confusing placement of non-null assertions next to comparison or assignment operators.
✅ This rule is included in the ts stylisticStrict presets.
When a non-null assertion (!) appears immediately before an assignment (=) or comparison operator (==, ===, in, instanceof), it can be visually confused with a different operator.
For example, a! == b looks similar to a !== b, and a! = b looks similar to a != b.
This visual ambiguity can make code harder to understand and may cause readers to misinterpret the intended logic.
Examples
Section titled “Examples”declare const value: string | null;
value! == "test";declare let value: string | null;
value! = "new value";declare const key: string | null;declare const object: Record<string, number>;
key! in object;declare const value: object | null;declare class MyClass {}
value! instanceof MyClass;declare const value: string | null;
value! == "test";declare const value: string | null;
value !== "test";declare const key: string | null;declare const object: Record<string, number>;
key! in object;declare const value: object | null;declare class MyClass {}
value! instanceof MyClass;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your team is familiar with non-null assertions and the visual similarity does not cause confusion in your codebase, you may choose to disable this rule. Projects with established conventions around non-null assertion placement may also opt out.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.