- Error
- Reported by the type checker
TS2722
Cannot invoke an object which is possibly 'undefined'.
A value that may be undefined is called, under strictNullChecks. Check it first, or call it with ?.().
What it means
The callee's type includes undefined (for example an optional method or callback), so calling it directly could fail at runtime.
Example
function run(cb?: () => void) {
cb();
}
export {};
ts-r-check -p tsconfig.json --pretty false ts-r outputindex.ts(2,3): error TS2722: Cannot invoke an object which is possibly 'undefined'.
Recorded by node scripts/verify-examples.mjs with ts-r built from commit 6deaf01. The example uses the default tsconfig: strict, target: es2022, module: esnext, moduleResolution: bundler.
How to fix
- Check before calling:
if (cb) cb();. - Use optional call:
cb?.(). - Make the parameter or property required if it is always provided.