Recibo el error anterior con el código de ejemplo a continuación, soy bastante nuevo en las operaciones ternarias anidadas, por lo que agradecería su ayuda. Código de ejemplo a continuación:
get notEmptyProduct(): string[] { return this.contractSettings.allowedRegularEstimateProducts && this.contractSettings.allowedRegularEstimateProducts.length ? this.contractSettings.allowedRegularEstimateProducts : this.contractSettings.allowedControlEstimateProducts && this.contractSettings.allowedControlEstimateProducts.length ? this.contractSettings.allowedControlEstimateProducts : []; }
Extraiga esta operación ternaria anidada en una declaración independiente.
Puede utilizar el encadenamiento opcional y cómo javascript procesa los valores Falsy y Truthy . La última matriz vacía es para asegurarse de que devuelve una matriz vacía en caso de otras fallas para cumplir con el tipo de retorno del método. Esto es mejor que tener operadores ternarios anidados, creo.
get notEmptyProduct(): string[] { return (this.contractSettings.allowedRegularEstimateProducts?.length && this.contractSettings.allowedRegularEstimateProducts) || (this.contractSettings.allowedControlEstimateProducts?.length && this.contractSettings.allowedControlEstimateProducts) || []; }
Intente mover el ternario anidado a una variable separada.
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; }