Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript will not let you call a function that might be undefined

Errorerror TS2722: Cannot invoke an object which is possibly 'undefined'.

Short answer

TS2722 means the value you are calling has the type 'function | undefined'. That happens with optional props and optional parameters when strictNullChecks is on. Either check the value before the call, use the optional call form fn?.(), or remove the question mark so callers must pass it.

  • Applies to TypeScript 5.x, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Hover the name in the line the error points at. If the type ends in '| undefined', it is declared optional (a '?') and you are calling it unchecked. Then confirm strict mode is on, because that is what makes the compiler report it.

    grep -n "strict" tsconfig.json
    npx tsc --noEmit
  2. 2Match what you see, then jump to its fix

    1. 1onChange?: (value: string) => void in the props type, and onChange(value) in the bodyAn optional callback prop is called without a checkFix
    2. 2Several optional callbacks, each wrapped in the same if blockYou want a short form that does nothing when the callback is missingFix
    3. 3Every caller already passes the functionThe value is not really optionalFix

What does the error look like?

  • error TS2722: Cannot invoke an object which is possibly 'undefined'.
  • Cannot invoke an object which is possibly 'undefined'.ts(2722)

Cause 1 of 3

An optional callback prop is called without a check

1Check

Look at the props interface. A property with a question mark can be undefined, and with strictNullChecks on, TypeScript reports it when you read it and then call it.

grep -n "?:" src/components/*.tsx

2Fix

Test the value first. Inside the if block the type is narrowed to the function, so the call is allowed.

type Props = { onChange?: (value: string) => void };

function Field({ onChange }: Props) {
  function handle(v: string) {
    if (onChange) {
      onChange(v);
    }
  }
  return <input onChange={(e) => handle(e.target.value)} />;
}

3Verify

Run the compiler. The error for that line is gone and no other errors appeared.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 3

You want a short form that does nothing when the callback is missing

1Check

Decide whether skipping the call is the right behaviour when the caller passed nothing. If yes, the optional call syntax says exactly that.

grep -n "onChange\|onSubmit\|onClose" src/components/*.tsx

2Fix

Put ?. before the argument list. The call runs only when the value is not null or undefined.

function Dialog({ onClose }: { onClose?: () => void }) {
  return <button onClick={() => onClose?.()}>Close</button>;
}

3Verify

The file compiles, and clicking the button with no onClose given does nothing instead of throwing.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 3 of 3

The value is not really optional

1Check

If the component cannot work without the callback, the question mark in the type is the mistake, and the guard only hides it.

grep -rn "<Dialog" src

2Fix

Remove the question mark so TypeScript forces every caller to pass it, and no check is needed inside.

type Props = { onClose: () => void };

function Dialog({ onClose }: Props) {
  return <button onClick={onClose}>Close</button>;
}

3Verify

Compile again. Any caller that forgot the prop is now reported, which is the point.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

How do I stop it coming back?

  • Keep strict on in tsconfig.json so these mistakes show in the editor and not as runtime crashes.
  • Mark a prop optional only when the component has a sensible behaviour without it.
  • Avoid the non-null assertion (!) to silence this error, because it moves the failure to runtime.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.