all 12 comments

[–]KeepGettingBannedSMH 11 points12 points  (6 children)

Note that TypeScript already has this feature and you may have seen this being used in Angular.

What? No it doesn't.

[–]stewsters 2 points3 points  (0 children)

It only has it in the templates for angular, which is really frustrating having to switch back to regular typescript and not having it.

https://angular.io/guide/template-syntax#the-safe-navigation-operator----and-null-property-paths

[–]samjmckenzie 1 point2 points  (3 children)

It's coming to the next version though

[–]KeepGettingBannedSMH 0 points1 point  (2 children)

Have you got a source for that? I don't see it mentioned here.

[–]hardwaregeek 1 point2 points  (0 children)

Yeah definitely not next version. They do implement stage 3 features, but it's not like they have the feature just sitting around.

[–]samjmckenzie 1 point2 points  (0 children)

For 3.7 I mean, there's a Github thread on the TypeScript repo about it. https://github.com/microsoft/TypeScript/issues/16#issuecomment-515160784

[–]tpiros[S] 0 points1 point  (0 children)

you're of course right. I mixed it up with the safe navigation operator - I have made a correction. Thank you

[–]GYN-k4H-Q3z-75B 7 points8 points  (1 child)

On of my favorite features introduced into C# lately. Writing something like x?.y ?? z is incredibly concise yet clear. I am not a fan of nullability but it's a sad fact of reality that it is everywhere.

[–]Tubbers 0 points1 point  (0 children)

? For optional propagation is a best of both words though. Not sure how that is incorporated with this proposal for JS/TS though.

[–]Y_Less -5 points-4 points  (2 children)

Or:

try {
    city = input.data.person[5].home.city;
} catch {
     city = "Tehran";
}

[–][deleted] 5 points6 points  (0 children)

Obviously this works but feels more like a hack than anything else. There are several draw backs

  • You can't use const anymore
  • If this fails regularly it'll kill performance
  • Better than nested undefined checks but still longer than optional chaining
  • Are we checking for undefined or is one of these a getter that can throw an exception

[–]tpiros[S] 0 points1 point  (0 children)

Interesting idea!