TypeScript Strict Null Checks (2026)

The Error

error TS2531: Object is possibly 'null'.
  src/services/user.ts:24:15 - error TS2531
    const name = user.profile.displayName;
                 ~~~~

This error appears when Claude generates code that does not account for strictNullChecks: true in your tsconfig.json. Claude may produce code that accesses properties without null guards.

The Fix

  1. Add null guards to the generated code:
// Before (fails strict null):
const name = user.profile.displayName;
// After (passes strict null):
const name = user?.profile?.displayName ?? 'Unknown';
  1. Tell Claude about your strict config in CLAUDE.md:
echo '- TypeScript strict mode is ON. Always use optional chaining (?.) and nullish coalescing (??).' >> CLAUDE.md
  1. Verify the fix compiles:
npx tsc --noEmit

Why This Happens

Claude generates code based on the prompt and context, but without access to your full tsconfig.json, it defaults to non-strict patterns. When strictNullChecks is true, TypeScript requires explicit null handling for every potentially null value. Claude’s generated code often assumes values exist, causing compile errors in strict projects.

If That Doesn’t Work

  • Add type assertions where null is impossible by design:
const name = (user.profile as NonNullable<typeof user.profile>).displayName;
  • Use a type guard function:
function assertDefined<T>(val: T | null | undefined, name: string): asserts val is T {
  if (val == null) throw new Error(`${name} is null`);
}
assertDefined(user.profile, 'user.profile');
  • Run Claude with strict context: include the tsconfig.json in your prompt.

Prevention

Add this to your CLAUDE.md:

# TypeScript Rules
- strictNullChecks is ON. Always handle null/undefined.
- Use optional chaining (?.) for property access on optional types.
- Use nullish coalescing (??) for default values, never || for booleans/numbers.
- Run npx tsc --noEmit after every code change.

See Also

This fix also applies if you see variations of this error:

  • Connection or process errors with similar root causes in the same subsystem
  • Timeout variants where the operation starts but does not complete
  • Permission variants where access is denied to the same resource
  • Configuration variants where the same setting is missing or malformed

If your specific error message differs slightly from the one shown above, the fix is likely the same. The key indicator is the operation that failed (shown in the stack trace) rather than the exact wording of the message.

Frequently Asked Questions

Does this error affect all operating systems?

This error can occur on macOS, Linux, and Windows (WSL). The exact error message may differ slightly between platforms, but the root cause and fix are the same. macOS users may see additional Gatekeeper or notarization prompts. Linux users should check that the relevant system packages are installed. Windows users should ensure they are running inside WSL2, not native Windows.

Will this error come back after updating Claude Code?

Updates can occasionally reintroduce this error if the update changes default configurations or dependency requirements. After updating Claude Code, verify your project still builds and runs correctly. If the error returns, reapply the fix and check the changelog for breaking changes.

Can this error cause data loss?

No, this error occurs before or during an operation and does not corrupt existing files. Claude Code’s edit operations are atomic — they either complete fully or not at all. However, if the error occurs during a multi-step operation, you may have partial changes that need to be reviewed with git diff before continuing.

How do I report this error to Anthropic if the fix does not work?

Open an issue at github.com/anthropics/claude-code with: (1) the full error message including stack trace, (2) your Node.js version (node --version), (3) your Claude Code version (claude --version), (4) your operating system and version, and (5) the command or operation that triggered the error.

Try it: Paste your error into our Error Diagnostic for an instant fix.