Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

278
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda