There was a massive issue discussion on the TS repo about the "elvis" operator, or safe null coalescing in js. I don't have the link, but a js implementation went to stage 2 IIRC for this exact thing.
Js needs this, as
x && x.y && x.y.z
or
((x || {}).y || {}).z
are obviously terrible.
You could write a simple function using try catch to do this, or prototype on object as well:
There was a massive issue discussion on the TS repo about the "elvis" operator, or safe null coalescing in js. I don't have the link, but a js implementation went to stage 2 IIRC for this exact thing.
Js needs this, as
or are obviously terrible.You could write a simple function using try catch to do this, or prototype on object as well:
then
Also note - works for null but not undefined.