Open bpasero opened 5 years ago
For simple cases this seems redundant, but for more complex cases this is necessary. In the case of properties there could be a getter with logic behind it, and in that case you wouldn't want it to be evaluated twice.
For example:
class Fubar {
public get nested(): { value: boolean } {
console.log("nested accessed.");
return { value: true };
}
}
function isTrue(something: { nested: { value: boolean } | undefined }): boolean {
return something.nested?.value === true;
}
isTrue(new Fubar());
Without the local variable you would get two console log statements, but with the local variable only one.
I would guess there is no special distinction whether this is necessary or not, and personally I doubt the worth of such a check.
Yeah I was not thinking it should be changed in all cases, only for the simple ones if possible.
TypeScript Version: 3.7.x-dev.201xxxxx
Search Terms: optional chaining
Code
Expected behavior: Output:
Actual behavior: Actual Output: