# Fix Forbidden Non-Null Assertion in TypeScript ESLint

> Replace the TypeScript non-null assertion operator with the optional chain operator to satisfy the typescript-eslint no-non-null-assertion rule

**URL:** https://sentry.io/answers/how-to-fix-the-forbidden-non-null-assertion-in-typescript-and-react/

---

## The Problem

When using [typescript-eslint](https://typescript-eslint.io/) in a JavaScript application, you may encounter the following error:

```
@typescript-eslint/no-non-null-assertion
warning: Forbidden non-null assertion
```

The following example code would cause this error:

```javascript
const loggedInUsername = "bob";

const users = [
  { name: "Steven", age: 12 },
  { name: "Lisa", age: 32 }
];

const loggedInUser = users.find((u) => u.name === loggedInUsername);
console.log(loggedInUser!.age);
```

You can see a live demo of this error in [this typescript-eslint playground](https://typescript-eslint.io/play/#ts=5.4.3&fileType=.tsx&code=MYewdgzgLgBANiA5ogpgEwJJgKoRQJzAEMBbFGAXhgCIAjEW6gbgFgAod0SWAVz3wiUYAbXYwYAbxjEyALhoBlKCgBuKMNQA0MIqnkBGAEwwAvprGTppFPOoAZAJYQiWnXpgBmY2fYBdVhxsXNDwSKiYOPxCfAQQAHQAZg5gaAAUqTwAlJQAfDA8cTLkFCWhyOhYuARFmQHBIHAocQiIqS3hlfwAhHG6KLXsQA&eslintrc=N4KABGBEBOCuA2BTAzpAXGUEKQAIBcBPABxQGNoBLY-AWhXkoDt8B6Jge1s6e4XloBDZMkTR8lDk3RQx0DtEgRwYAL4hVQA&tsconfig=N4KABGBEDGD2C2AHAlgGwKYCcDyiAuysAdgM6QBcYwAviNUA&tokens=false).

For this error to occur, the `eslintrc` file has the ["no-non-null-assertion" rule](https://typescript-eslint.io/rules/no-non-null-assertion/) set to `"error"`:

```json
"rules": {
  "@typescript-eslint/no-non-null-assertion": "error"
}
```

The [Non-null Assertion Operator (Postfix `!`)](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#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. When your TypeScript configuration is set to do [`"strictNullChecks"`](https://www.typescriptlang.org/tsconfig/#strictNullChecks), use the non-null assertion operator to bypass the `null` and `undefined` type checks. It should only be used if you know a specific value won't be `null` or `undefined` at runtime.

## The Solution

Your error message may include a solution to the error:

```
Consider using the optional chain operator `?.` instead. This operator includes runtime checks, so it is safer than the compile-only non-null assertion operator.
```

To fix the example code above, use the [optional chain operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) when accessing the `age` property of the `loggedInUser`:

```typescript
console.log(loggedInUser?.age);
```

If the `age` property does not exist on the `loggedInUser` object, the expression will result in `undefined` instead of throwing an error.

---

*Source: [sentry.io/answers/how-to-fix-the-forbidden-non-null-assertion-in-typescript-and-react/](https://sentry.io/answers/how-to-fix-the-forbidden-non-null-assertion-in-typescript-and-react/)*
