0

I have got error

Property 'pricePerItem' does not exist on type 'ShipProductDetail'.

(the type of product is ProductDetail | ShipProductDetail)

but I've used sort of Type Guards

    <span
        *ngIf="product.hasOwnProperty('pricePerItem')"
        class="text-uppercase smaller"
      >
        {{ product.pricePerItem.priceWithVat | i18nNumber | ogCurrency }}
    </span>

Is it a bug or not?

UPDATE

It's not a bug its my fault :)

I worked it out with

hasPricePerItem(
    product: ProductDetail | ShipProductDetail
  ): PriceAndVatModel | null {
    if ((product as ProductDetail).pricePerItem) {
      return (product as ProductDetail).pricePerItem;
    }
    return null;
  }

<span *ngIf="hasPricePerItem(product)" class="text-uppercase smaller">
        {{ hasPricePerItem(product)?.priceWithVat | i18nNumber | ogCurrency }}
</span>
user3887366
  • 2,226
  • 4
  • 28
  • 41

1 Answers1

1

Unfortunately, typeguard does not work with ngIf. And hasOwnProperty is not a type-guard

HTN
  • 3,388
  • 1
  • 8
  • 18