我是打字稿的新手,具有以下代码接口定义。
export Interface Response {
content?: {
// ...
}
}
let response: Response | null = something();
如何在不进行空检查的情况下访问内容,我认为这根本不是最好的方法。
if (response != null) {
if (response.content != null) {
content = (response.content as MyContent);
}
}
答案 0 :(得分:0)
content = response?.content == null ? null : response.content 作为 MyContent;
答案 1 :(得分:0)
我运行的是 3.0.x
版的 typescript,它不支持 Safe Navigation Operator。
更新打字稿可以做类似的事情。
response?.content?.something