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
2 answers

Object destructuring or optional chaining which is better code?

Given there are two ways to write the code, which way is better in terms of effective code?const { match: { params: { clientId = '' } } } = this.props;const clientId = this.props?.match?.params?.clientId ?? ''N.B. We can ignore the fact that any of…
1
vote
1 answer

Why is TypeScript showing "Cannot invoke an object which is possibly 'undefined'.ts(2722)" error after optional chaining operator?

During this stayhome period I decided to dive into TypeScript and started to practice it by implementing some basic data structures. I am trying to implement a custom stack that uses custom nodes. My StackNodes are defined like this: class StackNode…
zaplec
  • 1,681
  • 4
  • 23
  • 51
1
vote
1 answer

A little question about optional chaining in swift

I run these codes in the playground, but I get a compile error with a.b.b = 3 but it runs well at the next line. What's the difference between these two? When should I use the '!' explicitly and when it's not necessary? Here are the codes: class A…
richar lee
  • 11
  • 1
1
vote
2 answers

"no-unused-expressions" with TypeScript 3.7 optional call

I'm using TypeScript 3.7 with @typescript-eslint/parser and @typescript-eslint/eslint-plugin for linting. I'm trying to use optional chaining syntax and it works fine beside optional calls. const { hj } = window; hj?.('formSubmitFailed'); //…
Tomasz Mularczyk
  • 34,501
  • 19
  • 112
  • 166
1
vote
1 answer

optional chaining operator working on local host but not in production

I'm not sure if this something I'm doing wrong, or that I missing something, But I've added in optional chaining ?. to some of the parts of my create react app. i.e const customFieldName = customFields[0]?.customFieldName || "Custom…
1
vote
0 answers

How to resolve error "constructors in/after an Optional Chain are not allowed"

I meet a problem when I'm integrating plugin @babel/plugin-proposal-optional-chaining, and it throws error like this: constructors in/after an Optional Chain are not allowed. How to resolve this problem? Thanks.
1
vote
2 answers

How can I try TypeScript 3.7's optional chaining today?

I installed the latest TypeScript nightly (Version 3.7.0-dev.20190924) so I could try optional chaining (?.) support, but I am getting error TS1109: Expression expected. Is there a compiler flag I need to set besides strict = true?
nielsbot
  • 15,922
  • 4
  • 48
  • 73
1
vote
1 answer

Why can't I call map(_ transform: (Wrapped) -> U) -> U? and use optional chaining at the same time?

I know that optional chaining like this: someOptional?.someProperty is basically someOptional.map { $0.someProperty } However, I found that doing both at the same time is not possible: // someOptional?.someProperty evaluates to an optional type,…
Sweeper
  • 213,210
  • 22
  • 193
  • 313
1
vote
2 answers

Force unwrap or use optional chaining to set property

Given an optional var pinImageView: UIImageView? which has properties that are set, I'm not sure which way is preferred to set the property. For example: if let image = UIImage(named: "Pin") { pinImageView = UIImageView(image: image) …
bhartsb
  • 1,316
  • 14
  • 39
1
vote
3 answers

Optional chaining and coalescing incl. function arguments

Optional chaining lets us make decisions on the existence of objects: var text : String? let len = text?.lengthOfBytes(using: .utf8) ?? 0 Which will always set len to an integer. Is something similar possible for non-optional function arguments?…
Eiko
  • 25,601
  • 15
  • 56
  • 71
1
vote
2 answers

What are '!' and '?' marks used for in Swift

When I changed from Objective-C to Swift programming, I have come across '!' (exclamation mark) and '?' (question mark) that often have to be put right after a property, a method call etc. What are these marks used for, and what happens if we did…
Burak
  • 525
  • 4
  • 24
1
vote
1 answer

What is the difference when change to use optional chaining replace forced unwrapping in swift?

When call a function of an object instance, the object may be not exist(optional type), it seems like you can always put an question mark behind the object name, instead of put an exclamation mark behind the object name, and not crash.…
zgjie
  • 2,099
  • 2
  • 21
  • 32
1
vote
1 answer

Optional chaining for constructor calls?

I want to optionally cast an NSNumber? to an Int?, but the initialiser method for Int only takes init(NSNumber), so I can't pass an NSNumber?. Is there a way for me to compact this code so that it uses something like optional chaining? // number:…
vrwim
  • 13,020
  • 13
  • 63
  • 118
1
vote
1 answer

Call back method not calling from UIView to ViewController in Swift

I have two classes CustomSwipOut which is a subclass of UIView and ViewController subclass of UIViewController. The callback method of delegate is not firing in ViewController class from didSelectRowAtIndexPath delegate method of table view defined…
LC 웃
  • 18,888
  • 9
  • 57
  • 72
0
votes
1 answer

Use Java 8 Optional features in the multiple null conditions

Use Java 8 Optional features in the multiple null conditions The conditions is Optional payee = Optional.ofNullable(paymtTrans.getPayee()); if (payee.isPresent() && payee.get().getPayeeAcc() != null &&…
Kishore_2021
  • 645
  • 12
  • 38