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

406
Views
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 answers
Answer question

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 Report

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 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!