Enable Optional Chaining in TypeScript

by Tommy Leung on March 3rd, 2020

You may have heard that TypeScript 3.7 added support for optional chaining.

The update suggests that you could just start using it without turning on experimental flags or anything like that.

Unfortunately, I got this error from Parcel when I tried to use it:

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

A prolonged session of Googling led me no where. Was I the only idiot having this problem? 😭

I even tried switching to FuseBox to make sure the problem wasn't with Parcel. It wasn't.

The solution was simple and obvious in hindsight.

We have to make sure the value for target in tsconfig results in transpiling that polyfills optional chaining.

I was using esnext so that wasn't going to work. đŸ¤Ļ‍♂ī¸

Switched to es2016 and Parcel no longer complained. Most of us are probably targeting es5 and won't run into this problem.

For the rest of us just update tsconfig.json like this:

{
	"compilerOptions": {
		"target": "es2019",
		// ...
	}
}

If you want my software engineering discoveries and insights delivered then drop your email in the box below!