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
const value: string | null
value
: string | null;
const value: string | null
value
! == "test";
declare let
let value: string | null
value
: string | null;
let value: string | null
value
! = "new value";
declare const
const key: string | null
key
: string | null;
declare const
const object: Record<string, number>
object
:
type Record<K extends keyof any, T> = { [P in K]: T; }

Construct a type with a set of properties K of type T

Record
<string, number>;
const key: string | null
key
! in
const object: Record<string, number>
object
;
declare const
const value: object | null
value
: object | null;
declare class
class MyClass
MyClass
{}
const value: object | null
value
! instanceof
class MyClass
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 ❤️‍🔥 around the world by the Flint team and contributors.