typescriptsap-commerce-cloudspartacus-storefront

Property 'unit' does not exist on type 'Product' in SPARTACUS


I want to create a new attribute in the Product DTO in Spartacus. I have followed the documentation and created a new TS file and added code as below

declare module '@spartacus/core' {
  export interface Product {
    unit?: string;
  }
}

But I am not able to refer this in my HTML. Getting error Property 'unit' does not exist on type 'Product'.

Is there anything else I have to do to this work.

If I do as below then it works

export interface CustomProduct extends Product {
  unit?: string;
}

But By doing the above I have to replace it everywhere in the codebase from Product to CustomProduct.

This issue happens in PDP and Cart Page. Is there any Normalizer which I need to override?


Solution

  • Please make sure to import the file with the module augmentation from other file that is taken into compilation. See docs:

    Note: In any file where a module is augmented, there must be at least one import from the module. It can even be an unused import.

    (source: https://sap.github.io/spartacus-docs/type-augmentation/#augmenting-modules)

    Alternatively, the "hack" with importing this file shouldn't be necessary if only you rename the file with module augmentation to have an extension .d.ts instead of .ts