1

We are upgrading our system to angular 9.1.1, which has Typescript 3.8.3. The @angular-devkit/build-angular@0.901.1 use core-js@3.6.4 underneath.

We would like to start using the optional chaining feature in Typescript rather than lodash _.get e.g.

object?.a?.b?.c

rather than
_.get(object, 'a.b.c', 'default');

However, we still need to support 30% of the IE users. Does anyone know if it's support by IE by default? If not, is there anyway that we can turn on support, e.g. adding configure in polyfill.ts? I searched through the core-js and didn't see anything related to optional chaining.

Thanks a lot!

Annie Huang
  • 161
  • 1
  • 7
  • 1
    Turned out it's doesn't required polyfill if you are using Typescript 3.7 or above.Checked in Safari and IE (not support according to [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining) and they both work. Typescript must have transpile into the correct JS syntax. – Annie Huang Apr 25 '20 at 00:14
  • 1
    You could post it as an answer and mark your answer as an accepted answer. It can help other community members in future in similar kind of issues. Thanks for your understanding. – Yu Zhou Apr 27 '20 at 01:37

2 Answers2

2

The optional chaining operator is in Stage 4 which hasn't reached cross-browser stability. It's not supported by IE.

You could try to use @babel/plugin-proposal-optional-chaining to transform it. It can transform optional chaining operators into a series of nil checks.

Yu Zhou
  • 11,532
  • 1
  • 8
  • 22
0

See this example on TypeScript Playground:

const obj: {
    outer?: {
        inner?: string
    }
} = {};

console.log(obj?.outer?.inner);

Playground Link

Even when targeting ES2017, the optional chaining operator is compiled to basic JS code which all current browsers understand.

So no need to worry :)

mperktold
  • 436
  • 4
  • 14