Questions tagged [optional-chaining]

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null. Use for questions about such chaining with a appropriate language tag like [swift], [JavaScript], etc.

Optional chaining is a process for querying and calling properties, methods, and subscripts on an optional that might currently be nil/null . If the optional contains a value, the property, method, or subscript call succeeds; if the optional is nil/null , the property, method, or subscript call returns nil/null.

JavaScript Optional chaining operator - ?.

173 questions
4
votes
3 answers

Optional chaining and await

Is there any reason to use if to check if method exists, before calling await if (someObject.save){ await someObject.save(); } rather than using nullish coallescence diectly with await await someObject.save?.(); Is second code a safe…
husayt
  • 14,553
  • 8
  • 53
  • 81
4
votes
1 answer

Is it a good practice to use optional chaining inside reacts useMemo/useEffect dependencies?

I'm working on a large react app where performance matters. At first I avoided using objects properties inside useMemo dependencies (I was avoiding dot notation inside dependencies). But I have seen this in react's documentation so I think it is…
gkpo
  • 2,623
  • 2
  • 28
  • 47
4
votes
2 answers

Why javaScript's Optional Chaining Syntax does not work with Nullish Coalescing Operator when there is no method's return value?

I"m trying the new JavaScript Optional Chaining syntax and it seems that it has an issue with methods which has no return value. For example: I have a restaurant object with order method in it: const restaurant = { order(i, k) { console.log(`I…
Irfan
  • 4,882
  • 12
  • 52
  • 62
4
votes
3 answers

How to use optional chaining while searching through a dictionary in swift?

I want to safely search through values in a swift Dictionary using if lets and making sure it is type safe as I get deeper and deeper into the dictionary. The dictionary contains dictionaries that contains NSArray that contains more dictionary. At…
Jacky Wang
  • 618
  • 7
  • 27
4
votes
3 answers

Is Swift optional chaining always done with the if let construction, or is it just done using a question mark with an optional?

As per Apple docs, optional chaining is the following: You specify optional chaining by placing a question mark (?) after the optional value on which you wish to call a property, method or subscript if the optional is non-nil. ... optional chaining…
stackdaemon
  • 309
  • 4
  • 10
3
votes
3 answers

optional chaining in javascript causing error in eslint

i tried to use optional chaining in javascript but my eslint rules causing error . Error : Unsafe usage of optional chaining. If it short-circuits with 'undefined' the evaluation will throw TypeError no-unsafe-optional-chaining const { homeAddress,…
V2rson
  • 137
  • 1
  • 10
3
votes
3 answers

optional chaining in Angular Template

Compiled with problems:X ERROR src/app/components/users/users.component.html:2:22 - error TS2532: Object is possibly 'undefined'. 2
    ~~~~~~~~~~~~~~~~~ …
Ric Bocad
  • 45
  • 1
  • 5
3
votes
1 answer

Google Apps Script - optional chaining throwing ParseError

Why doesn't google Apps Script support latest javascript features like optional chaining even though it's running on Chrome V8? When optional chaining is used, clasp is throwing an error. GaxiosError: Syntax error: ParseError: Unexpected token . The…
Safal Pillai
  • 1,567
  • 1
  • 16
  • 31
3
votes
3 answers

JSHint suppress error for optional chaining

I was writing some Javascript when I found out about optional chaining (?.). I decided that I would need it in some code I was writing. When I finished typing out the code, I noticed that JSHint was giving me an error that stated Expected an…
shreyasm-dev
  • 2,711
  • 5
  • 16
  • 34
3
votes
2 answers

Optional chaining in combination with toLowerCase and indexOf during array map

For a autocomplete input I search in project properties during typing. Now I want to improve this code filterProjects(value: string) { return this.projects.filter( project => project.key.toLowerCase().indexOf(value.toLowerCase().trim())…
Jean D.
  • 169
  • 3
  • 15
3
votes
0 answers

support optional chaining and nullish oprtor in nx.dev

At the first step I added "typescript": "~3.7.5" and then I added "prettier": "1.19.1" and it should've been enough, but I still getting this error: 'optionalChaining' isn't currently enabled so I tried to add "react-scripts":…
3
votes
1 answer

Swift: compile-time error with optional chaining and nil-coalescing

I have a strange problem with optional chaining and nil-coalescing in Swift. Can anyone explain why the following code won't compile: class A { var val: T var x: A? var y: A? init(t:T){ val = t } func test()…
tierriminator
  • 587
  • 5
  • 19
3
votes
1 answer

dynamicType of optional chaining not the same as assignment

Optional chaining returns always an optional value. To reflect the fact that optional chaining can be called on a nil value, the result of an optional chaining call is always an optional value, even if the property, method, or subscript you are…
Binarian
  • 12,296
  • 8
  • 53
  • 84
2
votes
2 answers

How does stacking the optional chaining (question-mark-dot) operator in Javascript behave?

I ran these experiments in the Chrome console: a = {} -> {} a.n.n.n.n.n.n.n -> Uncaught TypeError: Cannot read properties of undefined (reading 'n') a?.n.n.n.n.n.n.n -> Uncaught TypeError: Cannot read properties of undefined (reading…
Martin Geisse
  • 1,189
  • 1
  • 9
  • 22
2
votes
1 answer

Use optional chaning to increment object property

I want to increment an object value using the optional chaning operator (?.). In my example I want to increase the age of a Person by one in a function called birthday() only if an age is set for the current person. The line with the if clause works…
Fulligan
  • 75
  • 4
1 2
3
11 12