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

Type assertions and optional chaining in Typescript

I have the following function that performs a simple type assertion, checking that a variable is not undefined or null. const isDefinedAndNotNull = (val: T | null | undefined): val is NonNullable => (val !== null) && (typeof val !==…
James Paterson
  • 2,652
  • 3
  • 27
  • 40
2
votes
2 answers

Why am I getting "Object is possibly 'undefined'" in this optional chain?

I got here: buyTicketData?.pricingOptions this error: [tsl] ERROR in /Applications/MAMP/htdocs/wp-content/plugins/tikex/tikexModule/components/BuyTicket/PricingOptionInvoiceItemsFormFieldsCheckboxes.tsx(280,25) TS2532: Object is possibly…
János
  • 32,867
  • 38
  • 193
  • 353
2
votes
1 answer

Use optional chaining operatori in Nodejs 14.x

i try to use optional chaining operator in Nodejs, i have window 10 with nvm installed. I have try use my script with node 14.5 and 15 but the response it's always the same C:\Users\user1\AppData\Roaming\nvm\v15.6.0\node64.exe…
Luca Bottoni
  • 81
  • 1
  • 9
2
votes
0 answers

How to use optional chaining in vite?

I found the question How to use optional chaining in Node.js 12. And there's a problem, it doesn't work in my vite@2.4.4 project. It actually works fine on iphone12, but doesn't work in my another android mobile. Then I used edge://inspect…
ubbcou
  • 49
  • 10
2
votes
1 answer

You may need an appropriate loader to handle this file type. When i use optional chaining operator in my create-react-app project

I learning redux right now, and i was making a project where i was doing a google oAuth authentication. so when i tried to use optional chaining operator i get this error. You may need an appropriate loader to handle this file type. | …
2
votes
1 answer

Type check for getElementById only works with optional chaining, not with type check

When trying to add a gradient to a line chart, I needed to get it's canvas. Before proceeding with the canvas I added a typecheck, however Vetur remarked that the "Object is possibly 'null'.Vetur(2531)" mounted() { const canv =…
Hui Gui
  • 123
  • 6
2
votes
1 answer

Iterating Into a Table from a complex JSON

I am struggling with optional chaining in JavaScript to map a JSON object to a Table in React. This is the payload: { "originating request": { "id":1, "name":"ali", "markets": [ { "name":…
2
votes
1 answer

Typescript does not understand undefined return type when used with optional chaining

I have a very simple code: const foo = (state: RootState): MyData => undefined; This gives an error: Type 'undefined' is not assignable to type 'MyData'. Which is quite reasonable since my type MyData does not allow undefined. But if I now write…
Ilya Chernomordik
  • 27,817
  • 27
  • 121
  • 207
2
votes
2 answers

Optional chaining for interfaces that have an index signature

I defined two interfaces. The first one has an optional field, the second one has an index signature: interface A { foo?: { bar: number }; } interface B { [s: string]: { bar: number }; } Why does the first interface give me a result of type…
2
votes
2 answers

Can I use Optional chaining to give a default value for the property of an optional?

Is it possible to use optional chaining and nil coalescing simultaneously like so? print("Meeting host: " + meeting.host?.email ?? “No host”) I'd like to do this but I get an error saying that my String? is not unwrapped. email is a non optional…
Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
2
votes
1 answer

Is Swift optional chaining lazily evaluated left-to-right?

The documentation doesn't seem to mention anything explicit about the order of optional chaining evaluation, just that: Multiple queries can be chained together, and the entire chain fails gracefully if any link in the chain is nil. Might seem…
sleep
  • 4,855
  • 5
  • 34
  • 51
2
votes
3 answers

Get the negative of an Optional Chain

This is not allowed (bad expression) if !(let nsDictionaryObject = swiftObject as? NSDictionary) { "Error could not make NSDictionary in \(self)" return } Is it possible to check for the negative of an Optional Chain expression in 1 line?
Aggressor
  • 13,323
  • 24
  • 103
  • 182
2
votes
1 answer

Optional Chaining returning an Int

Here is how I'm returning the number of rows for a table view: public func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { if let dataCount = self.data?.count { return dataCount } else { return…
bandejapaisa
  • 26,576
  • 13
  • 94
  • 112
1
vote
0 answers

Can't bypass optional chaining error using Babel or Webpack

I am having an issue when I am trying to use the aws-sdk v3. I am confident it is because of the optional chaining used in this node module file. ERROR in ./node_modules/@aws-sdk/signature-v4/dist-es/getCanonicalHeaders.js 10:30 Module parse failed:…
mg123
  • 11
  • 2
1
vote
1 answer

Efficient way to check if a getter will return null in Java before passing to Optional?

I have a simple data class such as @Data @Builder public class MyPerson implements Serializable { String name; String age; //Purposely keeping as String here String address; } After constructing the object, I want to construct an…
mitriola
  • 57
  • 5