0

I have this line in my code:

this.type = options.type == null ? MessageType.Success : options.type;

I am wondering if there is any syntax in TypeScript that makes such a thing shorter. Just because it is used a lot.

Is it something like this?

this.type = options.type ?? MessageType.Success;

I've already seen that, but I don't know what it means.

blaumeise20
  • 1,787
  • 2
  • 17
  • https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator – Aplet123 Feb 25 '21 at 17:33
  • https://dev.to/laurieontech/nullish-coalescing-let-falsy-fool-you-no-more-41c0 – Ravi Ashara Feb 25 '21 at 17:36
  • `??` is the *nullish coalescing operator*. It evaluates the left-hand operand and if the resulting value is `null` or `undefined`, it evaluates its right-hand operand and takes that value as its result. So in `this.type = options.type ?? MessageType.Success`, `this.type` gets assigned `options.type` if that is not either `null` or `undefined`, or `MessageType.Success` if it is. – T.J. Crowder Feb 25 '21 at 17:36
  • @Aplet123 Ah, thank you! – blaumeise20 Feb 25 '21 at 17:36

0 Answers0