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
1
vote
3 answers

How to make multiple operation in ifPresent with Optional on Java?

What I do is simply user can do trade but first I need to check balance of accounts(usdt & bitcoin account). Simple thing is that for buying I only check usdt account balance and for selling I only check crypto balance but the problem is that these…
abidinberkay
  • 1,857
  • 4
  • 36
  • 68
1
vote
0 answers

Property property of OptionalMemberExpression expected node to be of a type ["Identifier"] but instead got "CallExpression"

I have a line of code that looks something like this: return someObj.getSomething(param).doSomethingElse().key.anotherKey All of my jest unit tests pass. In order to be a bit safer, I added optional chaining to that line so now it looks…
noblerare
  • 10,277
  • 23
  • 78
  • 140
1
vote
1 answer

Is there a way to utilize the nullish coalescing operator (`??`) in object property destructuring?

In ReactJS, I commonly use this pattern of destructurnig props (I suppose it is quite idiomatic): export default function Example({ ExampleProps }) { const { content, title, date, featuredImage, author, tags, } =…
HynekS
  • 2,738
  • 1
  • 19
  • 34
1
vote
1 answer

Null checks for elements with optional chaining

Is it good practice to do null checks with optional chaining like the following example? document.querySelector('.foo')?.classList.add('bar'); In many codebases I see this: let el = document.querySelector('.foo'); if(el){ …
ivi_does_it
  • 155
  • 1
  • 8
1
vote
0 answers

Why does Swift 5 optional chaining for safeAreaInserts require two question marks in window??.safeAreaInsets instead of one question mark?

The Swift compiler complains about let insets : UIEdgeInsets? = UIApplication.shared.delegate?.window?.safeAreaInsets with the error message Value of optional type UIWindow? must be unwrapped to refer to member safeAreaInsets of wrapped base type…
1
vote
2 answers

Should we use multiple optional chaining operators for same object or if statement?

I'm working on an Angular2 app and I often encounter the following problem: I have an object of some type that could possibly be undefined, and I need to assign properties of that object to multiple variables or another object properties, so I…
tsinevik
  • 11
  • 1
  • 2
1
vote
0 answers

enable optionalChaining plugin for Prettier Now extension?

// Error below from Prettier You can only use optional-chaining when the 'optionalChaining' plugin is enabled.
1
vote
0 answers

Npm building fails because of optional chaining

I am developing a project using laravel, so that means I'm using mix to compile javascript. I have syntax like return this.page?.sidebar_text_color When I run npm run dev or npm run watch, I get compilation error Add…
Matrix
  • 437
  • 5
  • 18
1
vote
1 answer

Optional-chaining in Angular

I'm using optional chaining on an object which in turn gives this error after compiling the code For example const someObj = {pet_animals: {'dog', 'cat'}}; const duplicate = someObj?.wild_animals; tsconfig file Error message
Prime
  • 11
  • 4
1
vote
0 answers

Node.js 14 optional chaining and spread operator in array

Here is a snippet of my code const data = [ ...prev, ...resp.data ] This crashes if resp.data is not defined. With the new chaining operator syntax I have found following solution how to handle the case that resp.data is undefined. const data = […
Jirmed
  • 373
  • 2
  • 13
1
vote
2 answers

How does the JavaScript optional chaining(?.) operator works?

I was looking into the new JS optional chaining?. operator. It helps us from getting errors like this, TypeError: Cannot read property ‘x’ of undefined You can write const obj = {a: 1}; console.log(obj?.b); without an error. It will return…
Md Sabbir Alam
  • 4,937
  • 3
  • 15
  • 30
1
vote
1 answer

CRA optional chaining not working even with react-script 3.4.3

Optional chaining is not working in CRA even with react-script 3.4.3 e.g. modelClass.withId(payload.modelRef.id)?.delete(); throws the following error: Expected an assignment or function call and instead saw an expression …
ali9091
  • 35
  • 6
1
vote
1 answer

SwiftUI Picker selection parameter giving "Expression type 'Binding<_>' is ambiguous without more context" error

I'm getting a Expression type 'Binding<_>' is ambiguous without more context error at $order.quantity.contracts in the following code: struct ContractsPickerView: View { @Binding var order: Order var question: String …
Vee
  • 1,821
  • 3
  • 36
  • 60
1
vote
2 answers

Angular 9 with Typescript 3.8.3 is default support optional chaining in IE without polyfill (core-js) changes?

We are upgrading our system to angular 9.1.1, which has Typescript 3.8.3. The @angular-devkit/build-angular@0.901.1 use core-js@3.6.4 underneath. We would like to start using the optional chaining feature in Typescript rather than lodash…
1
vote
0 answers

With && operator, React showing 0

I am using the below statement to conditionally render a component. {data?.length && } data - an array. RenderThisComp - a component that needs to be rendered only if data array has any value. If the length of data array is zero,…