TypeScript 3.7 版本已实现了此功能

    1. {
    2. // before
    3. const street = user && user.address && user.address.street;
    4. const num = user && user.address && user.address.getNum && user.address.getNum();
    5. const arr = user && user.address && user.address.dataList && user.address.dataList[0] && user.address.dataList[0].details;
    6. console.log(street, num);
    7. }
    8. {
    9. // now
    10. const street2 = user?.address?.street;
    11. const num2 = user?.address?.getNum?.();
    12. const arr2 = user?.address?.dataList?.[0]?.details;
    13. console.log(street2, num2);
    14. }