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

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



Solution 1:[1]

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

Solution 2:[2]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 MrSasaN
Solution 2 Manfred