Does TypeScript have a Null-conditional operator

Yes, as of Typescript 3.7 you can now do this via optional-chaining

person?.getName()?.firstName

gets transpiled to

let firstName = person === null || person === void 0 ? void 0 : (_person$getName = person.getName()) === null || _person$getName === void 0 ? void 0 : _person$getName.firstName;

Note the check for null. This will work as expected if for example person is defined as

let person:any = null; //no runtime TypeError when calling person?.getName()

However if person is defined as

let person:any = {};//Uncaught TypeError: person.getName is not a function

See also this similar stackoverflow question

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)