最佳答案
TypeScript 3.7现在支持 可选链接操作符,因此,您可以编写以下代码:
const value = a?.b?.c;
I.e., you can use this operator to access properties of an object, where the object itself may be null or undefined. Now what I would like to do is basically the same, but the property names are dynamic:
const value = a?[b]?.c;
然而,我得到了一个语法错误:
错误 TS1005: “ :”预期。
我到底做错了什么,这有可能吗?
建议书似乎暗示这是不可能的(但也许我把语法示例弄错了)。