24

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?

3
  • 1
    sounds like you are using create-react-app. Last time I checked this doesn't support optionalChaining in the babel config it uses. See this issue for updates: github.com/facebook/create-react-app/issues/7940 Commented Nov 25, 2019 at 22:24
  • @BenClayton thanks! Hope they will release that update asap. Could you post your comment as an answer, so I could mark it as solved? Commented Nov 25, 2019 at 22:31
  • I've added an answer Commented Nov 27, 2019 at 10:31

2 Answers 2

22

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.

Sign up to request clarification or add additional context in comments.

Comments

17

Solved with a release of the CRA v3.3.0. If someone still has same error as in the title make sure you've upgraded react-scripts package.

1 Comment

Updating is pretty quick and easy too: yarn add --exact [email protected]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.