Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript ECMAScript 6 - ERROR: "You can only use decorators on an export when exporting a class"

I am trying to export a function in ECMAScript 6 so I can import it and use it in other files to have DRY code.

However, I receive the following error:

You can only use decorators on an export when exporting a class (16:0) while parsing file:

@idempotent
export function totalItems() {
    this.cart.items.forEach((dish) => total += item.qty);
    return total;
}

How can I fix this problem?

like image 405
Hamza L. Avatar asked Jan 05 '23 21:01

Hamza L.


1 Answers

What you're trying to do is not supported. The decorator proposal (which is not yet standardized) only includes @decorators for classes definitions and their methods, not for function definitions.

You may want to create a function decorator/wrapper function to be used like this instead:

export const totalItems = idempotent(function() {
    let total;
    this.cart.items.forEach((dish) => total += item.qty);
    return total;
});
like image 142
Jeremy Avatar answered Jan 16 '23 07:01

Jeremy