Does TypeScript have a Null-conditional operator

前端 未结 6 1291
误落风尘
误落风尘 2020-12-11 00:10

Is there any operator like ?. in TypeScript that can check if the variable is null or not defined like Kotlin? Like

person?.getName()?.firstName ?: \"None\"
         


        
6条回答
  •  再見小時候
    2020-12-11 00:38

    The actual good answer is in Andreas' comment. Your example would be implemented as below from Typescript 3.7 onwards :

    person?.getName()?.firstName ?? "None"
    

    See https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#nullish-coalescing

提交回复
热议问题