Cannot invoke an object which is possibly 'null'.
Production Risk
Build will fail; resolve before shipping.
A type-checking error (TS2721): Cannot invoke an object which is possibly 'null'.. This diagnostic is emitted by the TypeScript compiler when cannot invoke an object which is possibly 'null'..
- 1The value may be null or undefined at this point in the code
- 2Strict null checks are enabled and the type includes null or undefined
TypeScript compiler reports TS2721 during type checking.
// Triggers TS2721 // Cannot invoke an object which is possibly 'null'.
expected output
error TS2721: Cannot invoke an object which is possibly 'null'.
Fix
Add null check before accessing
WHEN When the value might be null or undefined
// Use optional chaining
const result = obj?.property;
// Or null assertion (only if certain it is non-null)
const result = obj!.property;
// Or narrow with a type guard
if (obj !== null && obj !== undefined) {
const result = obj.property;
}Why this works
Optional chaining (?.) returns undefined instead of throwing; nullish coalescing (??) provides fallback values.
✕ Use non-null assertion (!) without being certain the value is non-null
Non-null assertions bypass type safety; a null value will still cause a runtime error.
TypeScript Compiler Diagnostics
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev