Skip to content

SyntaxError: Optional chaining cannot be used with new

Quick summary

You wrote new?.Foo() — the parser rejects it because new and ?. cannot combine.

Why this happens

  • Trying to optionally construct an object with new?.

  • Refactoring a chain that mixed new and optional access

  • Misunderstanding where ?. can appear in an expression

Minimal example

✗ Broken code
const obj = new?.Foo(); // SyntaxError: Optional chaining cannot be used with new
✓ Fixed code
const Foo = null;
const obj = Foo ? new Foo() : null; // construct conditionally

new and ?. cannot combine because the construction target is ambiguous. Using a conditional that checks Foo before calling new is the safe pattern.

How to diagnose

  • Read the SyntaxError mentioning optional chaining and new

  • Find the new?. combination in the reported line

  • Separate the construction from the optional access

How to fix

  • Construct first, then use ?. on the result

  • Guard with a conditional before calling new

  • Use a factory function that returns null instead of new?.

How to prevent

  • Never combine new with ?. — separate the steps

  • Use a linter that flags new?. syntax

Related Resources

Related Glossary

Related Lessons

  • Objects

    Learn about optional chaining.

← Back to language