nonNullAssertedOptionalChains
Reports non-null assertions on optional chain expressions.
✅ This rule is included in the ts logical presets.
Optional chain expressions (?.) return undefined when the left side is null or undefined.
Using a non-null assertion (!) on an optional chain is contradictory: you’re asserting the result is not null or undefined, but the optional chain may have already returned undefined.
This pattern is almost always a mistake.
The non-null assertion doesn’t change the runtime behavior—if the chain short-circuits, the value will still be undefined, potentially causing runtime errors later.
Examples
Section titled “Examples”declare const object: { property?: string } | undefined;
object?.property!;declare const object: { method?: () => string } | undefined;
object?.method()!;declare const object: { nested?: { value: string } } | undefined;
(object?.nested)!.value;declare const object: { property?: string } | undefined;
object?.property;declare const object: { method?: () => string } | undefined;
object?.method();declare const object: { nested: { value: string } } | undefined;
object?.nested.value;declare const object: { nested?: { value: string } };
object.nested!.value;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”If your project is still onboarding to TypeScript’s strictNullChecks compiler option, or more generally has unusual needs for null values, you might not receive false reports from this rule.
You might consider using Flint disable comments and/or configuration file disables for those specific situations instead of completely disabling this rule.