@typescript-eslint/non-nullable-type-assertion-style
Require value! instead of value as T when the assertion only removes null and undefined
Also called typescript-eslint/non-nullable-type-assertion-style, typescript/non-nullable-type-assertion-style
If name is a string | undefined, then name as string and name! do the same thing. The ! is shorter, and it says exactly what you're claiming, which is that the value is there. It also doesn't repeat the type, so there's nothing to update when the type changes.
--fix rewrites the assertion to !.
Configure#
Off by default. This rule needs types, so turning it on makes bun lint type check your code first.
[lint.rules]
"@typescript-eslint/non-nullable-type-assertion-style" = "error" # or "warn"Examples#
function nameOf(names: Map<string, string>, id: string) {
return names.get(id) as stringUse a ! assertion to more succinctly remove null and undefined from the type.;
}function nameOf(names: Map<string, string>, id: string) {
return names.get(id)!;
}An assertion that does more than remove null and undefined can't be written with !, so it's allowed:
function toText(value: string | number | null) {
return value as string;
}When not to use it#
@typescript-eslint/no-non-null-assertion reports every !. Don't turn on both.