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
1 answer

Destructuring an optionally chained object, to get the ...rest?

I have this setup const { discard_me, ...rest } = some?.optional?.chaining; I end up with an error that discard_me doesn't exist, but that's expected if chaining is also non-existent. It seems like the optional chaining should cover the concerns of…
AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
0
votes
1 answer

Define what method to call based on value of Optional

Is it possible, to somehow alter the code below, so in case of no status value provided to return all the PaymentLog objects stored in the database instead of only the ones with status equal with 404? Basically, I would like if the status variable…
user13123978
0
votes
1 answer

Is there an equivalent of optionalChaining in Javascript ES2020?

As we know, ES2020 supports optionalChaining. For example, we can write codes like: let x = {a:{b:'c'}}; console.log(x?.a?.b); and we will get 'c'. But in some cases, we may want to use the expression like x[a][b] rather than x.a.b. Can we use …
Dong
  • 269
  • 1
  • 3
  • 10
0
votes
1 answer

Question about Optional chaining and Nullish coalescing JavaScript operators support in PhpStorm

I wanted to ask if anyone knows how one can support the JavaScript nullish coalescing operator (??) and the optional chaining operator (?.) in PhpStorm. Currently I am developing a project in react-native in PhpStorm, both operators work fine but…
0
votes
1 answer

Is there sugar with Optional chaining/nullish coalescing to prevent Typeerrors with say Array.map?

Is there any sugar to ensure that map will not typeerror using tools like optional chaining/nullishcoalescing? let x = {y: 1, z: 2}; x?.map(i => i); // Typeerror Array.isArray(x)?.map(i => i); // Typeerror let y = '1234'; y?.length && y.map(i =>…
Armeen Moon
  • 18,061
  • 35
  • 120
  • 233
0
votes
1 answer

Optional downcasting to another type if the first one fails

I have a class with a delegate of type UIViewController This delegate can be one of 2 subclasses of UIViewController. Both subclasses contain a method with the same name taking the same arguments. class TypeOne: UIViewController { method() { …
froggomad
  • 1,747
  • 2
  • 17
  • 40
0
votes
1 answer

Swift3 optional chaining and unwrapping with error handling

I am attempting to unwrap an optional using the guard let syntax. The goal with guard let is to unwrap optionals, aborting the procedure if .none/nil is ever encountered. I had thought that this meant that an optional could not be legally returned…
Cognitio
  • 410
  • 1
  • 4
  • 14
0
votes
0 answers

The returns is not conform to the method return type but it still works

The following code snippet is simplified from the Optional Chaining chapter of the official document. In short, the class Person has an optional stored property address of type Address. By offer some/all of the property buildingName, buildingNumber…
SLN
  • 4,772
  • 2
  • 38
  • 79
0
votes
1 answer

Why do I get error when I do optional chaining?

protocol textingprotocol : class { func didEnterText (text:String) } class secondViewController: UIViewController { weak var delegate:textingprotocol? @IBOutlet weak var txtField: UITextField? @IBAction func dismissButton(sender:…
mfaani
  • 33,269
  • 19
  • 164
  • 293
0
votes
2 answers

Printing without force-unwrapping property doesn't trigger runtime error in Swift

I have the below code and it works fine, but I was expecting the line print(john.residence!.numberOfRooms) to crash, as my numberOfRooms variable has nil value and I am not using forced wrapping when passing its value as argument to print(). Can…
Max
  • 5,380
  • 6
  • 42
  • 66
0
votes
2 answers

How to refer to navigation controller in Swift?

I have created a UINavigationController object and set it as the window's rootViewController property. The rootViewController of the UINavigationController object is a class called UINavigationMenuViewController. If I want to navigate from…
ppalancica
  • 4,236
  • 4
  • 27
  • 42
0
votes
1 answer

an unexpected error while writing a piece of code from Swift programming by Apple(optional chaining)

I wrote the following piece of code which is identical to Swift Programming Language by Apple and got an unexpected error saying : type'()' does not conform to protocol 'boolean type' , its location is shown in the following code: class Person1…
user3783574
  • 127
  • 9
-1
votes
3 answers

Using JavaScript's optional chaining to conditionally render an image from an API

I'm working on building a very basic news app using the New York Times' 'top stories' API. The API is well documented and there are plenty of examples of how to get stared online. But I'm stuck with conditionally rendering an image. Some of the…
joe44
  • 3
  • 2
-1
votes
2 answers

How do you get data showing with TypeScript Optional Chaining

I have this Try it Yourself TypeScript Optional Chaining example from W3Schools TypeScript Null & Undefined section in screenshot below. I can see that the purpose of the example is to show you that when data is undefined, it displays No Yard, but…
-1
votes
1 answer

Can optional chaining be explicitly found in some prototype?

[Disclaimer] I do not encourage anyone to do prototype modification in anyone's code, specially shared (or meant to play along with other's). This is bad practice (if not plain evil). Much like force-pushing (on non-purely-personal repos/branches)…
Ar3s
  • 2,237
  • 2
  • 25
  • 47
1 2 3
11
12