Why can't the left-hand side of an assignment expression use optional chaining?

#!/JoePea joe at trusktr.io
Wed Jul 15 05:39:05 UTC 2020


There's a good number of upvotes for it. It would be nice.
#!/JoePea

On Thu, Apr 30, 2020 at 1:13 PM Jordan Harband <ljharb at gmail.com> wrote:
>
> See https://github.com/tc39/proposal-optional-chaining/issues/18
>
> On Thu, Apr 30, 2020 at 1:08 PM #!/JoePea <joe at trusktr.io> wrote:
>>
>> I was hoping to avoid things like
>>
>> ```js
>> const baz = this.foo?.bar?.baz
>> if (baz) baz.lorem = 123
>> ```
>>
>> #!/JoePea
>>
>> On Thu, Apr 30, 2020 at 1:04 PM #!/JoePea <joe at trusktr.io> wrote:
>> >
>> > This is perfectly fine:
>> >
>> > ```js
>> > this.foo?.bar?.setBaz(123)
>> > ```
>> >
>> > but this is not:
>> >
>> > ```js
>> > this.foo?.bar?.baz = 123
>> > ```
>> >
>> > Why is that not allowed, but function calls are?
>> >
>> > I find myself often wanting to do assignments like that. When I am
>> > auto-completing things in VS Code (TypeScript), it automatically and
>> > awesomely inserts the `?` characters, only to fail once I write the
>> > assignment part.
>> >
>> > #!/JoePea
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> https://mail.mozilla.org/listinfo/es-discuss


More information about the es-discuss mailing list