Skip to content

nonNullAssertedOptionalChains

Reports non-null assertions on optional chain expressions.

✅ This rule is included in the ts logical and logicalStrict 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.

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;

This rule is not configurable.

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.

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