Suggestion: "safe navigation operator", i.e. x?.y 路 Issue #16 路 microsoft/TypeScript (original) (raw)

Description

@RyanCavanaugh

Current Status

Open questions


C# and other languages have syntax sugar for accessing property chains where null (or in our case, undefined) might be encountered at any point in the object hierarchy.

var x = { y: { z: null, q: undefined } }; console.log(x?.y?.z?.foo); // Should print 'null' console.log(x?.baz); // Still an error console.log(x.y.q?.bar); // Should print 'undefined'

Need proposal on what exactly we should codegen, keeping in mind side effects of accessors.


Edit by @DanielRosenwasser February 27, 2018: This proposal is also called the "null propagation" operator.