Search code examples
typescriptsonarqubeangular5

SonarQube displaying "Remove this unnecessary cast"


Why is SonarQube giving this error and how could I fix it?

Remove this unnecessary cast.

Promise.all([
  this.customerViewCmr4tProvider.getData(activeNumber),
  this.customerBillManagementProvider.getData(individualPublicId, billingAccountId, false),
  this.agreementProvider.getData(this.activeCustomerServiceProvider.getPhoneNumber(), this.activeCustomerServiceProvider.getCmsProfile()
  )]
)
  .then(
    (results) => {
      let customerViewRootCrm4tModel = new CustomerViewRootCrm4tModel();
      let customerBillModel = new CustomerBillRootModel();
      let agreementRoot = new AgreementRoot();

      if (results[0] instanceof CustomerViewRootCrm4tModel) {
        customerViewRootCrm4tModel = results[0] as CustomerViewRootCrm4tModel;
      }

      if (results[1] instanceof CustomerBillRootModel) {
        customerBillModel = results[1] as CustomerBillRootModel;
      }

      if (results[2] instanceof AgreementRoot) {
        agreementRoot = results[2] as AgreementRoot;
      }

      const numberDevices = this.customerViewCmr4tProvider.getProductByActiveNumber(customerViewRootCrm4tModel, activeNumber).length;
      const lastBill = this.customerBillManagementProvider.getLastCustomerBill(customerBillModel).amountDue.value;
      const agreement = this.getAgreementMonthsLeft(agreementRoot);
      this.sendGoogleAnalyticsPageView(numberDevices.toString(), agreement, lastBill);
    },
    () => {
      // error
      this.sendGoogleAnalyticsPageView("", "", null);
    }
  );

enter image description here


Solution

  • In any language that provides a similar error, Java, C#, whatever, the message simply means that the type of the variable you are casting is already the type you're trying to cast it to. They're simply telling you to remove the cast, so that instead of:

    customerViewRootCrm4tModel = results[0] as CustomerViewRootCrm4tModel;
    

    You should do this:

    customerViewRootCrm4tModel = results[0];