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

408
Visualizações
Nested ternary into an independent statement typescript?

I am getting the above error with the example code below, I am quite new to nested ternary operations so your help would be appreciated. Example code below:

  get notEmptyProduct(): string[] {
    return this.contractSettings.allowedRegularEstimateProducts && this.contractSettings.allowedRegularEstimateProducts.length ? this.contractSettings.allowedRegularEstimateProducts : this.contractSettings.allowedControlEstimateProducts && this.contractSettings.allowedControlEstimateProducts.length ? this.contractSettings.allowedControlEstimateProducts : [];
  }

Extract this nested ternary operation into an independent statement.

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

0

You can make use of Optional chaining and how javascript process Falsy and Truthy values. The last empty array is to make sure that it returns an empty array in case of other failures to comply with method's return type. This is better than having nested ternary operators I believe.

  get notEmptyProduct(): string[] {
    return (this.contractSettings.allowedRegularEstimateProducts?.length  && this.contractSettings.allowedRegularEstimateProducts) || (this.contractSettings.allowedControlEstimateProducts?.length && this.contractSettings.allowedControlEstimateProducts) || [];
  }
about 4 years ago · Juan Pablo Isaza Relatório

0

Try moving nested ternary to a separate variable.

notEmptyProduct(): string[] { 
const nestedTernaryResult = this.contractSettings.allowedControlEstimateProducts && this.contractSettings.allowedControlEstimateProducts.length ? this.contractSettings.allowedControlEstimateProducts : [];
return this.contractSettings.allowedRegularEstimateProducts &&  this.contractSettings.allowedRegularEstimateProducts.length ?     this.contractSettings.allowedRegularEstimateProducts : nestedTernaryResult;
} 

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