- 
                Notifications
    
You must be signed in to change notification settings  - Fork 13.1k
 
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
One of the new features in C# 6 is the new ?. operator which lets you avoid writing lots of boilerplate code when accessing nested properties. It would be nice if there was something similar in TypeScript. http://blogs.msdn.com/b/jerrynixon/archive/2014/02/26/at-last-c-is-getting-sometimes-called-the-safe-navigation-operator.aspx
Problem:
var g1 = null; 
var item = this.parent; 
if (item !== null && item !== undefined) { 
    item = item.child; 
    if (item !== null && item !== undefined) { 
        item = item.child; 
        if (item !== null && item !== undefined) { 
            g1 = item.child; 
        } 
    } 
} 
if (g1 !== null) {
    // do stuff
}
Proposed solution:
var g1 = this.parent?.child?.child?.child;
if (g1 !== null) {
   // do stuff
}
This should also work with the results of method calls and array indexing:
var hrefLength = nodeList.item(0)?.getAttribute("href")?.length;
userList[0]?.firstName = "player 1";
tenbits, NeLaurynas, philcockfield, lucasbasquerotto, goncharov and 12 more
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created