javascript - 检查对象可能是 'null' 或 'undefined'

标签 javascript node.js typescript

我有这段代码,我检查 hostel.country.address 是否为空,但仍然如此

return hostel.country.address &&
               hostel.country.address.internalEmployeeIdentifier !== null ||
               hostel.country.address.externalEmployeeIdentifier !== null;

我遇到这个编译问题

hostel.country.address



Object is possibly 'null' or 'undefined'.


- error TS2533: Object is possibly 'null' or 'undefined'.

最佳答案

return hostel.country.address &&
               hostel.country.address!.internalEmployeeIdentifier !== null ||
               hostel.country.address!.externalEmployeeIdentifier !== null;

应该可以。

祝你好运

关于javascript - 检查对象可能是 'null' 或 'undefined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59731407/

相关文章:

javascript - OpenLayers-3 - source.clear() 的正确用法是什么

javascript - 如何在php中ajax成功后生成图形?

node.js - 如何使用mongojs删除一条记录

node.js - Travis CI 没有使用正确的 node.js 版本

html - Flexbox:使内联组件扩展父容器的整个宽度

javascript - Angular2 如何使用 Javascript XMLHttpRequest 重定向到另一个组件?

javascript - Relay Modern 不会向 GraphQL Server 发送网络请求

javascript - 两栏三图组合布局

javascript - 在 Nodejs 的 Router 文件中公开 vs 创建对象

javascript - 如何将具有相同属性的对象合并到一个数组中?