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

173
Visualizações
Is there a way to always use Optional-chaining in Angular

I am working with an Angular app where the api response can be missing some expected data.

This can crash the app and an easy fix has been to use Optional-chaining;

foo.bar.baz => foo.bar?.baz

Is there a setting or way to treat all '.' as '?.' and would this be a bad idea for any reason?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

As far as i went through the official Angular & Typescript docs, There is no such option. And there shouldn't be.

Its definitely a bad idea. This will result in "Silent" bugs, that will be hard to find as the system grows.

e.g. if you have the following model:

interface ContactInfoDTO {
   email: string;
   phone: string;
}

interface UserDTO {
   name: string;
   contactInfo: ContactInfoDTO;
}

And you assume that every user in the system should have a valid contact info. When you access the email property user?.contactInfo?.email, you could end up with a bug, not knowing that something is actually wrong with your data, since no error was thrown.

Use Optional-chaining carefully, only where you know that some data might not be defined for a reason.

There is a nice article on the matter: https://medium.com/@a.hafez852/is-optional-chaining-really-a-good-addition-to-javascript-e4bbbc31909f

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