Skip to content

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.

declare const value: string | undefined;
value! ?? "default";
declare function getValue(): string | null;
getValue()! ?? "fallback";
declare const object: { property?: string };
object.property! ?? "missing";

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.