Skip to content

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.

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;

This rule is not configurable.

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.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.