Re: [WICG/webcomponents] Using attributes as properties (#913)

I'd rather keep the shape-defining aspects of a class declaration in the class body so that parsers, type-checkers, and the VMs can understand the class shape.

If decorators advance, I think some kind of built-in decorators would offer a nicer way of associating an attribute and property. I know people have posited a `@reflect` attribute before. I think an `@attribute` decorator would be easy to understand:

```ts
const attribute = Element.attributeDecorator;

class MyElement extends HTMLElement {
  @attribute({name: 'serial-number', type: Number})
  accessor serialNumber;
}
```

The question raised here would be how to populate `observedAttributes` from decorators. Decorators will be able to add metadata to a class, so a utility function could gather the attribute names. Maybe it's even possible that HTMLElement could have a default implementation of `static observedAttributes` that return this. I don't know how viable that it.

```ts
class MyElement extends HTMLElement {
  // 'foo' is an additional observed attribute not declared with a decorated field.
  static observedAttributes = ['foo', ...Element.getDeclaredAttributeNames(this)];
}
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/WICG/webcomponents/issues/913#issuecomment-786910070

Received on Friday, 26 February 2021 21:44:56 UTC