Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable optional chaining with Create React App and TypeScript

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

I was getting the above error. I followed this post and added "@babel/plugin-proposal-optional-chaining": "^7.7.4" into my devDependencies.

Then I am getting this error,

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

So I followed this post and added .babelrc file into my project's root

{
    "presets": ["react", "es2015","stage-1"],
    "plugins": ["transform-runtime", "transform-optional-chaining"]
}

This did not seem to do anything. I also heard someone mentioning that Create React App does not let you modify babel's configurations. So my question is how can I enable optional chaining without re-wiring the whole CRA?

P.S. I am using "typescript": "^3.7.2", or at least that is what my package.json says. I tried npm install to ensure it is updated. Not sure if CRA doing something weird underneath and using older version of TypeScript somehow.


EDIT: When I started the project with CRA, I believe we were using TypeScript: 3.6.x. I wanted to use Optional Chaining, so I changed my package.json file to "typescript": "^3.7.2" then npm install. I think the problem is, TypeScript knows that I am using 3.7.2, but CRA still have older configuration and I am not sure how I can update that.

like image 689
Hafiz Temuri Avatar asked Nov 28 '19 17:11

Hafiz Temuri


People also ask

How do you use optional chaining in react?

Using optional chaining on optional props This is the optional chaining operator which means if the property before it is null or undefined an error won't occur if its members are accessed. Instead, the expression will be automatically short-circuited, and undefined returned. Neat!

Does TypeScript have optional chaining?

Hence, since version 3.7, TypeScript has introduced optional chaining and nullish coalescing.

Can we use TypeScript in Create-react-app?

New App From Scratch If you're building a new app and using create-react-app , the docs are great: You can start a new TypeScript app using templates. To use our provided TypeScript template, append --template typescript to the creation command.

How do you add optional chaining?

To enable optional chaining, you need to install a package. At the time of writing, optional chaining is not natively supported in Javascript, it is a new feature introduced in ES2020. Until it is fully adopted we can get all the optional goodness by installing a package!


3 Answers

Create-React-App uses babel to transpile the TypeScript so it isn't using your npm installed version of TypeScript. Version 3.3.0 of react-scripts supports TypeScript 3.7. You can install it and use it with:

like image 94
LukeP Avatar answered Oct 18 '22 04:10

LukeP


package.json

{
  "scripts": {
    "start": "react-app-rewired start",
    "build": "react-app-rewired build",
    "test": "react-app-rewired test --env=jsdom"
  },
  "devDependencies": {
    "@babel/plugin-proposal-optional-chaining": "^7.2.0",
    "customize-cra": "^0.4.1",
    "react-app-rewired": "^2.1.3"
  }
  ...other
}

config-overrides.js

const { useBabelRc, override } = require('customize-cra');
module.exports = override(useBabelRc());

.babelrc

{
  "plugins": ["@babel/plugin-proposal-optional-chaining"]
}

detailed blogpost

like image 32
Medet Tleukabiluly Avatar answered Oct 18 '22 04:10

Medet Tleukabiluly


React scripts 3.3.0 and above supports it. There is no need to install the react-scripts@next.

Just put in the package.json "react-scripts": "^3.3.0" and it will work.

like image 22
Elisha Sterngold Avatar answered Oct 18 '22 05:10

Elisha Sterngold