7

I reduced the issue to this simple bit of code:

class X {
  static a = 5;
}

I don't know why this is a problem. It seems to be valid code. Any ideas?

Image of error

Adrian
  • 10,246
  • 4
  • 44
  • 110
  • 1
    I notice that static methods do _not_ throw a GAS syntax error (whereas static fields do). I didn't see anything pertinent in the [V8 runtime](https://developers.google.com/apps-script/guides/v8-runtime) notes. – andrewJames Jul 19 '21 at 22:20
  • So this is a bug then @andrewjames? – Adrian Jul 20 '21 at 02:13
  • I don't know. It could have some other explanation - maybe they are disallowed in GAS, for some reason. I did not see anything obvious in the Google [issue tracker](https://issuetracker.google.com). If you don't get an answer here, you could open a ticket. – andrewJames Jul 20 '21 at 12:47
  • 1
    As this seems to be a bug, I've reported it here: https://issuetracker.google.com/u/1/issues/194120497 – Adrian Jul 20 '21 at 23:27
  • There is a reply to your bug report ("won't fix"). I think it would be worth copying their notes & workarounds into an answer, here. Because you found the problem, asked the question, and opened the ticket, would you like to do that? – andrewJames Jul 31 '21 at 18:12
  • I'm closing this, as the bug report in the duplicate is more relevant and is still open. – TheMaster Feb 20 '22 at 15:50
  • Although Google Apps Script doesn't support static class fields, it does support static methods/getters/setters, so you can do this: `class X { static get a(){ return this._a || 0 } static set a(value){ this._a = value } }`, then you can get/set `X.a` as usual. – lochiwei May 18 '22 at 14:00

2 Answers2

9

Response from Google:


Apps Script V8 runtime supports EMCA2015 standards which mean static class properties are not supported at the moment. There are two workarounds available:

1 - Assign the property to the class:

// Caused an error
class MyClass {
  static a = 1;
}
// ECMA2015 compatible
class MyClass {}
MyClass.a = 1;

2 - Develop the Apps Scripts with Clasp: https://github.com/google/clasp which also supports TypeScript and many other new features.


Full details are in this ticket.

andrewJames
  • 19,570
  • 8
  • 19
  • 51
1

No static variables, but static functions are supported. So, technically you could use this:

class X {
  static a(){return 5;}
}
X.a();

It's not a variable.. but oh well, gets the job done!

Yeti
  • 5,628
  • 9
  • 45
  • 71