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.

AEdeclare consB0val7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0striA7<2|sp ansty="le--0:#C792EA;--1:#8844">AB0nu5<3;d ivclassNa="meec-li">53valuAE5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0stri5<3;d ivclassNa="meco">AEdeclare consB0val7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0numbA7<2|sp ansty="le--0:#C792EA;--1:#8844">AB0undefin5<3;d ivclassNa="meec-li">5<3B0numb53>valuAEdeclare consB0val7<2:sp ansty="le--0:#C792EA;--1:#8844">AB0striA7<2|sp ansty="le--0:#C792EA;--1:#8844">AB0numbA7<2|sp ansty="le--0:#C792EA;--1:#8844">AB0nu5<3;d ivclassNa="meec-li">53valuAE5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0stri5<3 sp ansty="le--0:#7FDBCA;--1:#096E">7<2|sp ansty="le--0:#D6DEEB;--1:#403F">5<3 sp ansty="le--0:#C5E478;--1:#3B61">B0numb5<3;d ivclassNa="meco">
44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st4<4AE spans="tyle--0:#7FDBCA;--1:#09">64<4AE spans="tyle--0:#C5E478;--1:#3B">61B033F53v4<4AE! spans="tyle--0:#D6DEEB;--1:#40">3<;di vdivclass="Nameexpressive-">44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4<4AE spans="tyle--0:#7FDBCA;--1:#09">64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0undef33F53v4<4AE! spans="tyle--0:#D6DEEB;--1:#40">3<;di vdivclass="Nameexpressive-">44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st4<4AE spans="tyle--0:#7FDBCA;--1:#09">64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4<4AE spans="tyle--0:#7FDBCA;--1:#09">64<4AE spans="tyle--0:#C5E478;--1:#3B">61B033F53v4<4AE! spans="tyle--0:#D6DEEB;--1:#40">3<;di vdivclass="Nameexpressive-">44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0st4<4AE spans="tyle--0:#7FDBCA;--1:#09">64<4AE spans="tyle--0:#C5E478;--1:#3B">61B0nu4<4AE spans="tyle--0:#7FDBCA;--1:#09">64<4AE spans="tyle--0:#C5E478;--1:#3B">61B033F53va44361B0st3<;di vdivclass="Nameexpressive-">44AEdeclare co61B0v64<4AE spans="tyle--0:#C5E478;--1:#3B">61B<0any spans="tyle--0:#D6DEEB;--1:#40">33F53va44361B0st3

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.