lit / lit-element

LEGACY REPO. This repository is for maintenance of the legacy LitElement library. The LitElement base class is now part of the Lit library, which is developed in the lit monorepo.
https://lit-element.polymer-project.org
BSD 3-Clause "New" or "Revised" License
4.49k stars 318 forks source link

static get properties() vs @property(): How to get properties when @property() is used. #1111

Closed GHNewbiee closed 3 years ago

GHNewbiee commented 3 years ago

When

  static get properties() {
    return {
      greeting: {type: String},
      data: {attribute: false},
      items: {type: Array},
    };

is used, then

<div>${Object.getOwnPropertyNames(this.constructor.properties).toString()}</div>

does work.

But when

  @property()
  greeting;

  @property({attribute: false})
  data;

  @property({type: Array})
  items;

is used, then TypeError: can't convert undefined to object. I suspect it refers to properties.

So, how can I get the names when @property() is used?

sorvell commented 3 years ago

See this example in the other issue you posted: https://github.com/Polymer/lit-element/issues/1102#issuecomment-738289694

Going to close this as we've added this capability to the next major version, and we're not going to address the issue in the current version given that you can work around the problem as shown above.

mscuthbert commented 9 months ago

Hi @sorvell -- this issue is still the top search engine result for iterating properties on a lit-element, and I'm on v3 now, but cannot find out how to list the properties if decorators are used. Can you point to how to do it on new lit versions? Thanks!

Ah! it is static get elementProperties() => Map<string, PropertyDefinition>

designbyadrian commented 3 months ago

I've got a class that extends LitElement from Lit 3.1.3, and elementProperties does not exist on the instance.

Edit: it's a static function, so can't access it on the instance.

justinfagnani commented 3 months ago

You can access static properties from the constructor:

this.constructor.elementProperties;