Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

ESLint: Forbidden non-null assertion (no-non-null-assertion) in a React project

ErrorForbidden non-null assertion

Short answer

The rule flags every use of the postfix ! operator because it tells the compiler a value is not null without proving it. Replace the ! with optional chaining and a fallback, or with an explicit check that narrows the type. If a line really needs it, disable the rule for that line only.

  • Applies to typescript-eslint current, ESLint 9, TypeScript 5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    See which config switches the rule on. The strict preset of typescript-eslint includes it.

    grep -rn "no-non-null-assertion\|strict" eslint.config.* .eslintrc* 2>/dev/null
  2. 2Match what you see, then jump to its fix

    1. 1The report sits on code such as user!.name or document.getElementById('root')!The ! operator is used where the value may really be nullFix
    2. 2Dozens of reports appear after enabling the strict preset or strictNullChecksThe project has many assertions and cannot fix them at onceFix

What does the error look like?

  • Forbidden non-null assertion
  • @typescript-eslint/no-non-null-assertion

Cause 1 of 2

The ! operator is used where the value may really be null

1Check

The ! tells TypeScript the value is not null or undefined, but nothing checks it at run time. The TypeScript handbook says the operator does not change runtime behavior.

grep -rn "[a-zA-Z)]!\." src/ | head

2Fix

Use optional chaining with a fallback, or check the value and handle the missing case.

// before
const len = user!.name.length;
// after
const len = user?.name.length ?? 0;

// before
const root = document.getElementById('root')!;
// after
const root = document.getElementById('root');
if (!root) throw new Error('Missing #root element');

3Verify

The linter passes and the type checker still agrees.

npx eslint src && npx tsc --noEmit

Confirmed by typescript-eslint.io

Did this fix it?

Cause 2 of 2

The project has many assertions and cannot fix them at once

1Check

The rule's documentation advises against turning it off completely, but accepts disable comments for the specific cases while types are being improved.

npx eslint src --format compact | wc -l

2Fix

Fix the lines you can, and add a disable comment on each line that must stay, with the reason.

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion -- value set in beforeEach
const store = testStore!;

3Verify

The remaining reports are only for lines without a comment.

npx eslint src

Confirmed by typescript-eslint.io

Did this fix it?

How do I stop it coming back?

  • Model optional data as optional in the types and narrow it where it is used.
  • Throw one clear error at the edge (for example for #root) instead of asserting everywhere.
  • Review any new eslint-disable comment in code review.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.