tc39 / proposal-grouped-and-auto-accessors

Grouped Accessors and Auto-Accessors for ECMAScript
https://tc39.es/proposal-grouped-and-auto-accessors
MIT License
54 stars 5 forks source link

Consider extending proposal to all class element definitions #9

Closed pzuraq closed 2 years ago

pzuraq commented 2 years ago

Following this thread, I think this proposal could be generalized to allow defining any type of class element in the class element block, not just accessors. This would add the ability set properties like writable and enumerable on class elements, and could also potentially be a reason to reconsider whether or not a syntactic opt-in is required (since this would be a much more general syntax than just auto accessors):

class C {
  identifier {
    writable?: boolean;
    enumerable?: boolean;
    configurable?: boolean;

    value?: unknown;
    get?: () => T;
    set?: (v: T) => void;
  }: T;
}

We could also have a much shorter opt-in keyword, define or def, instead of accessor:

class C {
  // `define` keyword
  define x { writable = false } = 123;
  @reactive define x { get; set; } = 123;

  // `def` keyword
  def x { writable = false } = 123;
  @reactive def x { get; set; } = 123;
}

I did a quick write-up of what this could potentially look like, it overlaps a lot with this proposal: https://gist.github.com/pzuraq/ae002a0f3e8745b57cd6c046bf9ff89f

rbuckton commented 2 years ago

I'm not comfortable with reifying property descriptors as an independent class element. If we want to give someone a way to modify other descriptor properties such as enumerability, I think we're better off introducing specific keywords or some kind of native decorator that has privileged access to the descriptor internally.