TypeScript 3.7.2 - Support for the experimental syntax 'optionalChaining' isn't currently enabled

flppv picture flppv · Nov 25, 2019 · Viewed 11.3k times · Source

In my project I am using TS 3.7.2 which should support optional chaining. But when I try to use it like that: const state = urlParams.state?.toString() I get the error:

Support for the experimental syntax 'optionalChaining' isn't currently enabled

Add @babel/plugin-proposal-optional-chaining (https://git.io/vb4Sk) to the 'plugins' section of your Babel config to enable transformation.

I checked release notes and didn't see any requirements about adding tsconfig options for that feature.

I am wondering if I need babel plugin & config when I am using TS already, and how should I fix the error?

Answer

Ben Clayton picture Ben Clayton · Nov 27, 2019

This sounds like you are using an older version of create-react-app.

Versions earlier than v3.3 don't support optionalChaining in the babel config it uses.

Support was added in CRA v3.3. See other answers for how to upgrade.