Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

275
Views
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 path/to/file.ts:12:34 - error TS1109: Expression expected.
[ng] 12         return array?.[0];
[ng]                         ~
[ng] path/to/file.ts:12:34 - error TS1003: Identifier expected.
[ng] 12         return array?.[0];
[ng]                          ~
[ng] path/to/file.ts:12:34 - error TS1005: ':' expected.
[ng] 12         return array?.[0];
[ng]                            ~

I don't understand why there is an error. According to https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining#array_item_access_with_optional_chaining the syntax would be correct. My IDE (vscode) also doesn't throws me an error

Array item access with optional chaining You can use bracket notation for optional chaining on arrays:

const arr = ['a', 'b', 'c', 'd']; let arrayItem = arr?.[42];

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

My typescript-version just seemed to be too old (3.4.3). Optional chaining was introduced in 3.7 (https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html), so it makes sense that it does not work in my old version.

I just assumed that my project was relatively up to date but turns out it wasn't.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!