Need help using field declarations inside class

Im getting this error and Im not sure why. Whats the right way to set total so that i can call checkout.total to return 0.

ps. im using typescipt

code

module.exports.Checkout = class {
    total: number = 0;
    scanItem() {
        return this.total;
    }
};

error

Details:

    /home/dominic/code/pillar/checkout-order-total-ts/src/api/checkout/checkout.ts:2
      total = 0;
            ^

    SyntaxError: Unexpected token =

It should work as the syntax looks valid to me.
Maybe there’s some other environment configuration that’s causing the issue?

Anyway since that compiles with a class with a constructor have you tried adding one manually?

    total: number;
    constructor() {
        this.total = 0;
    }

Maybe this way it will like it more. But it’s just a gut-feeling.

Hope it helps :+1:

that also works. i ended up updating to node 12.7.0 and that worked

Glad you sorted out :tada:

interface Checkout {
  total: number;
  scanItem(): number;
}


export default class Checkout {
  constructor() {
    this.total = 0;
  }

  scanItem() {
    return this.total;
  }
}
1 Like