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

how to write optional chaining with special character in key

I am trying to check if the key exists in the nesting level to avoid the error data={ payment-term:{ description:xyz description } } how I am fetching data[payment-term].description It works well with the above set of data but gets error on the…
MNSH
  • 55
  • 1
  • 8
0
votes
1 answer

Sencha Support for Optional Chaining

Does Sencha plan to support optional chaining? Or does Sencha already support it and perhaps I'm missing a setting to make it work?
Ron Grimes
  • 81
  • 1
  • 2
0
votes
1 answer

Enforce optional chain when accessing optional properties

I have a Typescript project, and I've found that when I access a property or function of a property that is optional, it doesn't error. For example: type Example = { bar?: string[] } const foo: Example = {} // No error, but the code will…
cogm
  • 275
  • 6
  • 17
0
votes
2 answers

Can Optional Chaining be alternative of 'in' operator in Javascript

I often saw in operator in some library of Javascript. but I think in operator has risk of causing human errors. because we have to write property name as string type. I think optional chaining could be alternative of in operator and It looks more…
0
votes
1 answer

Optional chaining arrays in Ionic Framework (typescript) throws error

I want to use optional chaining in my Ionic project but I receive errors even though I use the correct notation. My code // Get Array (might be empty) const array = await getArray(); return array?.[0]; My compiler throws 3 errors: ERROR in…
0
votes
1 answer

"Object is possibly 'null'." despite optional chaining

I am getting Object is possibly 'null'. ts(2531) even though I am using optional chaining and using TypeScript 4.6.3. const path = fileInput?.files[0]?.path tsconfig.json: { "compilerOptions": { "target": "esnext", …
leonheess
  • 16,068
  • 14
  • 77
  • 112
0
votes
1 answer

Using optional chaining with array.entries

Just trying to understand optional chaining and unsure if it can be used in the following situation with .entries, that is: for (const [index, val] of Array.from(myArray)?.entries()) { if (val.status === 1) { . . . . . . } } I…
ArthurJ
  • 777
  • 1
  • 14
  • 39
0
votes
1 answer

Is only one '?' property enough in my case with TypeScript Optional chaining

In my React and TypeScript (version "^3.9.9") app I am mapping over some items to render the list/teasers: {data?.getItems.items .map((teaser) => (
meez
  • 3,783
  • 5
  • 37
  • 91
0
votes
0 answers

Lint throwing errors for optional chaining only when running npm script

I have currently added optional chaining capability to my React project. This is now working fine when I run my project. But when I perform npm run lint, it still complains Unexpected token for following syntax. const myVar =…
karvai
  • 2,417
  • 16
  • 31
0
votes
2 answers

Java Optional with multiple commands and return object

Attempting to execute multiple commands and return an Object (ResponseEntity) using Optional.ifPresentOrElse More complex does not work: return ifPresentOrElse( repoService.findById(inId), i -> { …
zzDevzz
  • 13
  • 5
0
votes
3 answers

How to add optional chaining to JSON object?

I am trying to compare a value within a JSON object with a variable: if (resp.userdetails.name == username) { // do something } The problem is that not all resp objects have userdetails, so I get this error message sometimes: Cannot read…
user9847788
  • 2,135
  • 5
  • 31
  • 79
0
votes
1 answer

Prettier supports for Optional chaining (?.)

In my JavaScript React App I am using prettier for code formatting. My current rules are working as expected. I saw that Prettier supports also Optional chaining, but when I run prettier --write that does not change for example user.info.name into…
neman
  • 196
  • 1
  • 10
0
votes
1 answer

How to perform deep optional assignment in Javascript?

Say I have an object named foo, and I would like to perform the following operation: foo.bar.baz.qwer.asdf = 5; The issue is that bar may not exist, but if it does exist, then baz inside of it may not exist, etc. I thought I would be able to do…
0
votes
0 answers

Where are babel plugins stored on react tree? After npm install, plugin not in project tree

I'm not understanding why some babel packages appear within my node_modules, while others do not. I have "@babel/plugin-proposal-optional-chaining": "^7.16.5", in my babel.config.js in the root of the project tree. I did npm i --save…
smartblonde
  • 138
  • 1
  • 11
0
votes
1 answer

convert get of lodash javascript library to optional chaining

I wanted to change this code which is using get method from lodash library to optional chaining method.Can someone help me to convert this return get( this.UIObj, 'excelList', [] );