TypeScript Version: 3.7.2
Search Terms: optional chaining array items
Code
Is there any reason why I cannot do this:
interface MyInterface {
temp: number;
}
const a: Array<MyInterface | null> = [];
a[0]?.temp = 0;
and instead I'm forced to use something like this?:
interface MyInterface {
temp: number;
}
const a: Array<MyInterface | null> = [];
if (a[0])
a[0].temp = 0;
Expected behavior:
a[0] doesn't exists, so assigning number 0 will be skipped.
Actual behavior:
Error: The left-hand side of an assignment expression may not be an optional property access.(2779)
Related Issues:
This is not supported by the specification. See https://github.com/tc39/proposal-optional-chaining#not-supported
This is technically not the appropriate venue to discuss it (since it's just part of the ECMAScript feature, not TypeScript) - but just curious, what would you want to happen?
Do you want to create a property? Or do you want to not perform the assignment if it's not there?
I expected that assignment will be skipped and no property will be created (no side effects).
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes.
Most helpful comment
This is technically not the appropriate venue to discuss it (since it's just part of the ECMAScript feature, not TypeScript) - but just curious, what would you want to happen?
Do you want to create a property? Or do you want to not perform the assignment if it's not there?