nonNullAssertedNullishCoalesces
Reports non-null assertions on the left side of nullish coalescing operators.
✅ This rule is included in the ts logicalStrict presets.
The nullish coalescing operator (??) is designed to handle null and undefined values by providing a default when either is encountered.
Using a non-null assertion (!) on the left side of ?? is contradictory: you’re asserting the value is not null or undefined while simultaneously providing a fallback for when it is.
This pattern typically indicates programmer confusion about these operators, or copy-paste errors. The non-null assertion is always redundant in this context and can be removed.
Examples
Section titled “Examples”declare const value: string | undefined;
value! ?? "default";declare function getValue(): string | null;
getValue()! ?? "fallback";declare const object: { property?: string };
object.property! ?? "missing";declare const value: string | undefined;
value ?? "default";declare function getValue(): string | null;
getValue() ?? "fallback";declare const object: { property?: string };
object.property ?? "missing";declare const object: { nested?: { value: string } };
object.nested!.value ?? "default";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.