nonNullableTypeAssertions
Reports type assertions that can be replaced with non-null assertions.
✅ This rule is included in the ts stylistic 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.
Examples
Section titled “Examples”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;declare const value: string | null;value!;declare const value: number | undefined;value!;declare const value: string | number | null;value!;declare const value: string | number | null;value as string;declare const value: any;value as string;Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.