Why can't the left-hand side of an assignment expression use optional chaining?
# #!/JoePea (4 years ago)
I was hoping to avoid things like
const baz = this.foo?.bar?.baz
if (baz) baz.lorem = 123
#!/JoePea
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
# Jordan Harband (4 years ago)
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 > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20200430/f5c48e0e/attachment.html>
# #!/JoePea (4 years ago)
There's a good number of upvotes for it. It would be nice. #!/JoePea
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
This is perfectly fine:
this.foo?.bar?.setBaz(123)
but this is not:
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