Skip to content

nonNullableTypeAssertions

Reports type assertions that can be replaced with non-null assertions.

✅ This rule is included in the ts stylistic and stylisticStrict presets.

When asserting a type that only removes null or undefined from a union, a non-null assertion (!) is more concise and expressive than a full type assertion (as or angle-bracket syntax).

For example, if a value is of type string | null, asserting it as string can be replaced with a non-null assertion: value!.

This rule reports type assertions where the only difference between the original and asserted type is nullability, suggesting the use of a non-null assertion instead.

declare const value: string | null;
value as string;
declare const value: number | undefined;
<number>value;
declare const value: string | number | null;
value as string | number;

This rule is not configurable.

If your codebase prefers explicit type assertions for clarity or documentation, you may disable this rule. Alternatively, if you avoid non-null assertions entirely due to their potential for runtime errors, this rule may not be appropriate.

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