TypeScript 3.7 now supports the optional chaining operator. Hence, you can write code such as:
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;
However, there I get a syntax error:
error TS1005: ':' expected.
What am I doing wrong here? Is this even possible?
PS: The proposal seems to imply that this is not possible ?? (but maybe I get the syntax examples wrong).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…