BETA Shoulder is in beta — Findings may sometimes be wrong. Your feedback shapes what we fix next. Share feedback

Non-Null Assertion Without Null Check

Description

The non-null assertion operator (!) bypasses null/undefined checks at compile time without runtime safety, causing crashes when values are unexpectedly null.

What Shoulder detects

Non-null assertion (!), used on '{expression}' without explicit null/undefined check. This may cause runtime crashes if the value is null/undefined.

How to fix

Use explicit null checks or optional chaining instead of non-null assertions.

```typescript
function getUserEmail(userId: string) {
  const user = users.find(u => u.id === userId);
  if (!user) {
    throw new Error('User not found');
  }
  return user.email;
}
```

Learn more: https://shoulder.dev/learn/typescript/cwe-476/non-null-assertion

Applies to

Frameworks

express fastify nestjs next

References

Scan for this issue

Detect with Shoulder CLI
npx @shoulderdev/cli trust --rule=typescript-non-null-assertion .

Related rules