Home > Enterprise >  This condition will always return 'true' since the types 'string[]' and 'De
This condition will always return 'true' since the types 'string[]' and 'De

Time:03-28

hey guys im junior and need some help

        if (query.deliveryType && query.deliveryType != DeliveryTypeEnum.EITHER) {
            search.push({
                terms: {
                    "deliveryType.keyword": [query.deliveryType, DeliveryTypeEnum.EITHER],
                },
            });
        }

delivery type is string array

and error is :This condition will always return 'true' since the types 'string[]' and 'DeliveryTypeEnum' have no overlap. typescirpt (2367)

im getting error here query.deliveryType && query.deliveryType != DeliveryTypeEnum.EITHER

CodePudding user response:

Since query.deliveryType is an Array. you can use array functions like includes. so if your array of delivery Types did not include EITHER then you can what ever you want.

so you can do this:

if (query.deliveryType && !query.deliveryType.includes(DeliveryTypeEnum.EITHER)){
   // Do what ever you want!
}

You can learn more about includes function in this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

CodePudding user response:

The error tries to tell you that the if statement probably does not what you want it to do, because the type of the variable query.deliveryType already indicates that it will never be DeliveryTypeEnum.EITHER.

  • Related