Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

277
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda