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
0
votes
0 answers

Module parse failer due to an inappropriate loader when I'm trying to use optional chaining in Next.js

I'm creating some new feature in an app that is built with Next. I need to use optional chaining in some of my logic. When I introduce optional chaining I get the following error. Module parse failed: Unexpected token (371:52) You may need an…
Zach W.
  • 1
  • 1
  • 1
0
votes
3 answers

Is it possible to LOG different messages based on which step null is encountered while doing nested null check with Optional and map

I have a nested object which can return a null at any point of time. Thanks to Optional and map we can now do nested calls without having to put null checks after every get. I have a very unique requirement where I need to know at which step exactly…
Nick Div
  • 5,338
  • 12
  • 65
  • 127
0
votes
1 answer

Why doesn't the optional chaining operator (?.) work with Javascript assignment?

When I do this: var resetButton = document.querySelector('.resetButton'); resetButton?.onclick = function() { //... }; I got error: Uncaught SyntaxError: invalid assignment left-hand side. I would like to be able to use optional chaining…
Zorro
  • 1,085
  • 12
  • 19
0
votes
0 answers

Does Kotlin Have Something Similar to Swift's Guard-Let

In Swift I can do the following which would 1- make sure that none of these properties are nil and 2- give me access to them safely unwrapped: // user could be nil, and lat and lon are properties on the user object, they can also be nil.…
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
0
votes
0 answers

Object is possibly undefined in Typescript, even though it's inside an if statement that checks for truthiness

I received an error in React w/ Typescript says possiblyUndefined is possibly undefined. So I threw it in an if statement that specifics checks that it's defined before running the function inside the body. let [state, setState] = useState<{ foo:…
Cjmaret
  • 142
  • 9
0
votes
0 answers

Optional chaining property with 0 value

I have such code: if (action?.notificationKey && state.notifications[action.notificationKey]) { delete state.notifications[action.notificationKey]; } which does not satisfy me, because action.notificationKey can have 0 value and optional…
ddominoo
  • 11
  • 1
0
votes
0 answers

How to resolve need additional loader installation error?

I am stuck in an issue. I can't even start making the change to the project as its not even starting. installed all the dependencies and when i started by yarn start then i got this issue. I tried to solve this by downgrading my node to 14.21.3 I…
0
votes
2 answers

Using Optionals in Java 11 to avoid null pointer exceptions using functional chaining

I would like to use Java 11's Optional to protect myself from the dreaded null pointer exception. I wrote a basic test to check my understanding of how they work. like. so @Test public void optionalsWorkingAsExpected() { String x =…
codernoob8
  • 434
  • 1
  • 9
  • 24
0
votes
0 answers

Webpack parse failed - Unexpected token on optional chaining

./src/components/BookDetails/bookdetails.jsx 147:14 Module parse failed: Unexpected token (147:14) You may need an appropriate loader to handle this file type. | } | }, /*#__PURE__*/React.createElement("img", { > src: book?.cover_img, | …
bana2544
  • 1
  • 1
0
votes
0 answers

TypeScript compiler doesn't recognize check for optional object attribute in external function

I have a type definition with an optional member which I need to access in a function. In order to assure that the optional member is indeed set, I created a "validator"-function which throws an error if the optional member of the object is not set.…
0
votes
0 answers

null check / optional chaining for array contents?

we're all familiar with the tune?.image type of null checks in Typescript optional chaining docs but what's the best practice for checking this with an array value? tune.images?[0] will not work. I often use: tune.images ? tune.images[0] :…
dcsan
  • 11,333
  • 15
  • 77
  • 118
0
votes
0 answers

Optional chaining like syntax but cant any resource to explain

I have just come across this.person!.name in my codebase, previously I have seen this.person?.name which is optional chaining, but I have never seen ! be used nor can I find any resource to explain it's use, can anyone shed any light on it?
Udders
  • 6,914
  • 24
  • 102
  • 194
0
votes
1 answer

Is there a way to use optional chaning in qooxdoo?

I use qooxdoo 6.0 version. Optional chaing is a very useful feature, is there a way to use it in qooxdoo?
0
votes
1 answer

how to use optional chaining with map operator?

I have a scenario where I am reading a yaml file and based upon the data, make calls to tag aws resources. await Promise.all(doc?.SQS.map(async (queue)=>{ // code to tag 1 queue })) Now the yaml file may not have a any sqs, in which case doc?.SQS…
Naxi
  • 1,504
  • 5
  • 33
  • 72
0
votes
1 answer

It is really necessary to put the ? operator on every attribute of an optional chaining?

I have been working using optional chaining, but it looks like its enough by putting the ? operator on the penultime attribute to check the entire chain, what you think guys? In order to check the entire chain would be enough by using the optional…