Please add '@typescript-eslint/no-non-null-assertion': 'off' to your config file like below.

module.exports = {
  ...
  rules: {
    ...
    '@typescript-eslint/no-non-null-assertion': 'off'
  },
  ...
}
Answer from tatsuya kanemoto on Stack Overflow
🌐
TypeScript ESlint
typescript-eslint.io › rules › no-non-null-assertion
no-non-null-assertion | typescript-eslint
TypeScript's ! non-null assertion operator asserts to the type system that an expression is non-nullable, as in not null or undefined. Using assertions to tell the type system new information is often a sign that code is not fully type-safe.
🌐
TypeScript ESlint
typescript-eslint.io › rules › no-extra-non-null-assertion
no-extra-non-null-assertion | typescript-eslint
Some problems reported by this rule are automatically fixable by the --fix ESLint command line option. The ! non-null assertion operator in TypeScript is used to assert that a value's type does not include null or undefined.
🌐
TypeScript ESlint
typescript-eslint.io › rules › no-non-null-asserted-optional-chain
no-non-null-asserted-optional-chain | typescript-eslint
Using a ! non-null assertion to assert the result of an ?. optional chain expression is non-nullable is likely wrong. Most of the time, either the object was not nullable and did not need the ?. for its property lookup, or the ! is incorrect and introducing a type safety hole. ... export default tseslint.config({ rules: { "@typescript-eslint/no-non-null-asserted-optional-chain": "error" } });
🌐
Sentry
sentry.io › sentry answers › react › how to fix the forbidden non-null assertion in typescript and react?
How to fix the forbidden non-null assertion in TypeScript and React? | Sentry
The Non-null Assertion Operator (Postfix !) assertion removes the null and undefined types from a value. To use it, add the ! symbol after an expression like in the console log example above.
🌐
GitHub
github.com › typescript-eslint › typescript-eslint › blob › main › packages › eslint-plugin › docs › rules › no-non-null-assertion.mdx
typescript-eslint/packages/eslint-plugin/docs/rules/no-non-null-assertion.mdx at main · typescript-eslint/typescript-eslint
TypeScript's ! non-null assertion operator asserts to the type system that an expression is non-nullable, as in not null or undefined. Using assertions to tell the type system new information is often a sign that code is not fully type-safe.
Author   typescript-eslint
🌐
TypeScript ESlint
typescript-eslint.io › rules › no-non-null-asserted-nullish-coalescing
no-non-null-asserted-nullish-coalescing | typescript-eslint
Using a ! non-null assertion type operator in the left operand of a nullish coalescing operator is redundant, and likely a sign of programmer error or confusion over the two operators.
🌐
GitHub
github.com › typescript-eslint › typescript-eslint › blob › main › packages › eslint-plugin › src › rules › no-non-null-assertion.ts
typescript-eslint/packages/eslint-plugin/src/rules/no-non-null-assertion.ts at main · typescript-eslint/typescript-eslint
import type { TSESLint } from '@typescript-eslint/utils'; ... 'Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator.',
Author   typescript-eslint
Find elsewhere
🌐
GitHub
github.com › typescript-eslint › typescript-eslint › issues › 11559
Bug: [no-unnecessary-type-assertion] false positive when using non-null assertion (!) on inferred type · Issue #11559 · typescript-eslint/typescript-eslint
June 30, 2025 - module.exports = { parser: "@typescript-eslint/parser", rules: { "@typescript-eslint/no-unnecessary-type-assertion": "error" }, }; { "compilerOptions": { "strictNullChecks": true } } ... The linter reports an error: "This assertion is unnecessary since the receiver accepts the original type of the expression." However, if I remove the non-null assertion !, TypeScript complains.
Published   Sep 01, 2025
🌐
Reddit
reddit.com › r/typescript › new to ts - is this wrong?
r/typescript on Reddit: New to TS - Is this wrong?
July 26, 2022 -

Been new to TS and having only tried it long ago, I'm confused about some things.

I have a simple html page where I'm getting 2 elements from it, a button and a div, so I did:

const BUTTON: HTMLButtonElement | null = document.querySelector("#get");  
const DATA: HTMLDivElement | null = document.querySelector("#data");

Everything looks good so far, even when I know those wont be null, they could be for whatever reson I guess.

Then I have a listener so when the button is clicked I change the content on the div.

BUTTON?.addEventListener("click", async () => {  
 DATA ? DATA.textContent = "WORLD" : "";  
});  

I understand the need of using ? after BUTTON (Seams reasonable) but the part I bet I'm doing wrong is that DATA ? line... Is that what I need to write to have TS not complain about my code?

 DATA.textContent = "WORLD";

Object is possibly "null"

First my mind though... Ok you may be null so:

 DATA?.textContent = "WORLD";

That gives me The left-hand side of an assignment expression may not be an optional property access.

Then I remember I saw somewhere the ! for those cases.

DATA!.textContent = "WORLD";

And while I believe that's the "solution" I get a warning from eslint this time Forbidden non-null assertion.

Even then if DATA is null, code will throw an error, while my first piece of code wont.

What's the way to go here?

Top answer
1 of 5
13
document.querySelector returns Element | null because there's a chance that the selector ends up not matching on anything, so TypeScript forces you to handle that case explicitly. There are two ways to go about doing this: The first way is, if you're absolutely sure that the selector will always match on an element, you can use the non-null assertion operator, !. This way, you're basically telling the compiler: "Hey, I know better than you that this particular value will for sure never be null." Still, if your assumption turns out to be false for some reason, TS won't be able to protect you and you might run into runtime errors by trying to access properties on a value that's null. This is why the no-non-null-assertion ESLint rule exists. Some people argue that you should have a backup plan even if you're 100% sure the selector always matches, meaning you shouldn't use the ! operator at all. If you disagree with this stance, you can always turn off that particular rule in your ESLint config. The second way is to handle it the error case explicitly by doing something like this: if (DATA === null) { throw new Error("Could not find DATA element."); } DATA.textContent = "WORLD"; The TypeScript compiler is smart enough to figure out that, if execution manages to reach the line of code after the if block, DATA cannot be null at that point, so it's safe to access the textContent property.
2 of 5
6
It's awesome that TS can express that a variable may be T | null, and then checks for you to make sure you're handling the null (assuming strict mode is on). However, the problem as you've seen is that this "infects" all your core application logic, you have to clutter the things you're doing with null checks. Instead, consider pushing null checking to the boundaries of your application, and fail fast or handle those null cases. Then the rest of the application can work on the not-null case. For example here, is it legal for the button or data to be null? Could your app function if so? Consider instead to check these with if (!button) at the boundaries and crash/show message/log error or do whatever is appropriate. The rest of your app can be written in a way that it works on just a T, not a T | null.
🌐
GitHub
github.com › typescript-eslint › typescript-eslint › issues › 7693
Bug: [no-unnecessary-condition] false positive with non-null-assertions and noUncheckedIndexedAccess · Issue #7693 · typescript-eslint/typescript-eslint
August 8, 2023 - Violation of @typescript-eslint/no-unnecessary-type-assertion: This assertion is unnecessary since it does not change the type of the expression.
Published   Sep 25, 2023
🌐
Palantir
palantir.github.io › tslint › rules › no-non-null-assertion
Rule: no-non-null-assertion
Using non-null assertion cancels the benefits of the strict null checking mode.
🌐
Webdevtutor
webdevtutor.net › blog › typescript-eslint-forbidden-non-null-assertion
Avoiding Non-Null Assertion in TypeScript with ESLint
By setting "@typescript-eslint/no-non-null-assertion": "error", ESLint will throw an error whenever it encounters a non-null assertion in your code, prompting you to refactor and find a safer alternative.
🌐
Reddit
reddit.com › r/angular2 › eslint no-non-null-assertion in angular
r/Angular2 on Reddit: Eslint no-non-null-assertion in Angular
November 9, 2023 -

Hey,

how do you handle the no-non-null-assertion rule of Eslint? It's enabled per default now (strict mode of Angular) and I feel like it does not work all that well with Angular, especially with required Inputs, but also with forms and generated code (e.g. from an OpenAPI-specification file).

Eslint has no idea of Angular's live-cycle, so a component might have a required @Input foo, but it will still print an error in the whole typescript file if you access foo.bar, because it does not realize that their is a check for it. You can use optional-chaining in some cases (foo?.bar), but this is redundant and does not work for assignments. Or you can use the non-null-assertion (foo!.bar) but then Eslint will shout at you.

Sure, I can add a comment and disable the check every single time, but then my code is polluted with all that annoying comments.

Or I can disable the rule, but now our juniors can pick up that ball and run with it, even where non-nullness is NOT guaranteed by some previous checks.

How do you handle no-non-null-assertion?

🌐
GitHub
github.com › typescript-eslint › typescript-eslint › issues › 1195
[no-non-null-assertion] False positive with value that can have the value null · Issue #1195 · typescript-eslint/typescript-eslint
July 6, 2019 - Repro { "parser": "@typescript-eslint/parser", "parserOptions": { "project": "./tsconfig.json", "sourceType": "module" }, "plugins": ["@typescript-eslint"], "rules": { "@typescript-eslint/no-non-null-assertion": "error" } } function test...
Published   Nov 12, 2019
🌐
GitHub
github.com › testing-library › dom-testing-library › issues › 981
Conflicting eslint rules re: TypeScript non-null assertions · Issue #981 · testing-library/dom-testing-library
June 15, 2021 - 122:13 error Use a ! assertion to more succintly remove null and undefined from the type @typescript-eslint/non-nullable-type-assertion-style
Published   Jun 15, 2021