0

Is there a clean way to check for this ?

I am using typescript, and I have a big object with many optional value

I want to access,

value1.subValue1.subsubValue1;

but if subValue1 that is optional is undefined, I get an error.

For the moment, I only know something like

if(value1 && value1.subValue1 && value1.subValue1.subsubValue1);

But is there something shorter ?

value1?.subValue1?.subsubValue1 but it doesn't work in my code
Bobby
  • 4,372
  • 8
  • 47
  • 103
  • No, there's no "elvis" (i.e. null coalescing) operator. The way you did it is as short as it gets. – Jared Smith Feb 20 '19 at 03:05
  • but In some code for angular, there is the following : *ngIf="passwordForm?.controls.oldPassword?.errors?.required" – Bobby Feb 20 '19 at 03:07
  • Yes, it's duplicated, I didn't know that thing was called Elvis operator. now I can search for it thanks – Bobby Feb 20 '19 at 03:08
  • FWIW, I'd really like to see it added, but such is life. And yeah, I have no idea why they called it that other than it originated in the groovy language and I guess they thought it sounded cool. – Jared Smith Feb 20 '19 at 03:10

0 Answers0