Add experimentalOptionalChaining
property to compilerOptions
which will support at least static property access
โ the most required and expected feature.
For me, in 2019 the most important part of Optional Chaining is static property access. It will greatly improve DX for GraphQL client apps and make client code more robust and clean. ๐
Current shortcomings:
dottedPaths
as string ๐ฉ
_.get(obj, ['some.nested.field'])
with lodash.getobjectPath.get(obj, 'dottedPath')
with object-path (900k weekly downloads ๐ฑ)Assume we have following GraphQL response
from the server on the client side:
interface IResponse { viewer?: { article?: { title?: string } } } const response: IResponse = await apolloClient.query(`query { viewer { article(id: 15) { title } } }`);
On any level, graphql may return null
, so before displaying title
we must check viewer
and article
existence.
if-check-hell
๐คข
if (response && response.viewer && response.viewer.article && response.viewer.article.title) { // ... display title }
Pros: statically checked (if query changed, then we will be warned about un-existed props)
Cons: to much code
dottedPaths
as string ๐ฉ
if (_.get(response, ['viewer.article.title'])) { // ... display title }
Pros: quite clean code
Cons: statically UNchecked (if query changed, then we will NOT be warned about un-existed props checks)
if (response?.viewer?.article?.title) { // ... display title }
Pros: very clean code & static checks
Cons: not implemented in TS, but already implemented about a year ago in Flow 0.74, Eslint, Babel.
We just have 3 levels deep query, but in most cases it's deeper.
if (response && response.viewer && response.viewer.article && response.viewer.article.title) { if (_.get(response, ['viewer.article.title'])) { if (response?.viewer?.article?.title) {
Definitely, OptionalChaining is the winner ๐ฅ
Suggestion: repeat once moreJust add static property access
from optional chaining proposal under experimentalOptionalChaining
flag. This part is stable in the proposal and it is the most required feature in 2019.
Let's keep problematic optional chaining for function calling, short-circuiting, grouping, deletion, template literals โ out of the scope of this issue.
Please do not reference this issue as a duplicate for issue #16 from the 2014 year, here are 2019 and requirements changed from that time.
Search TermsOptional Chaining, null propagation.
My suggestion meets these guidelines:
If you like the fact that optional chaining was shaken off the dust from #16 and may even be implemented in the near future โ send a SWAG from your company to the following address
050010, Kazakhstan, Almaty,
Kabanbay Batyra 21 apt 3,
Mobile +7707234004
Pavel Chertorogov
or just give a ๐
kix, schickling, venil7, eweilow, nzvtrk and 239 morealexander-dobrodiy, Akiyamka, ykirill, strikepark, listochkin and 12 moremanojvigneshbmsdave, factormystic, frankrobert, manojvignesh, KleoPetroff and 1 morefrankrobert and IllusionMHschickling, gurschitz, eweilow, nzvtrk, evgenymarkov and 65 moreAwesomeObserver, AlexPetrov7311, gadfil, amotov, Voronar and 20 moreFlashbangSW, listochkin, 0xF6, jmenglis and mkraessig
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4