Polymer / polymer

Our original Web Component library.
https://polymer-library.polymer-project.org/
BSD 3-Clause "New" or "Revised" License
22.05k stars 2.02k forks source link

What about any property type in polymer ? #5597

Open divyanshu-rawat opened 4 years ago

divyanshu-rawat commented 4 years ago

So, I am basically a frontend developer from Angular, React background and have started to work with Polymer, since 1 month, Just like all JS frameworks and libraries, Polymer 3.0 also utilizes ES6.

So, for instance if I have a XCustom polymer class having a property userdeclared on it followed by its type.

So, if we talk about TS(typescript) then if we declare any property in a class and if we don't know its type then we just write any. like this user:any .

What about anyproperty type in polymer ?

  class XCustom extends Polymer.Element {
    static get properties() {
      return {
        user: String,
      }
    }
  }
jsilvermist commented 4 years ago

String or Object would probably be the best 2 choices depending on how you want to use it.

divyanshu-rawat commented 4 years ago

I know that but this Question is not about using them, it's about the types polymer supports.

arthurevans commented 4 years ago

Hi @divyanshu-rawat. The type key on a Polymer property is sometimes confusing because it serves a very different purpose than a TypeScript type.

The property type is used only when converting an attribute value to a property value. The process is described here:

https://polymer-library.polymer-project.org/3.0/docs/devguide/properties#attribute-deserialization

You can use any type you want in the properties object. For example, you could use a constructor for your own type (MyCustomType) or a string ('array-or-date').

If Polymer doesn't recognize the type, and you don't specify a custom deserializer, the attribute value is used unmodified (this is the same behavior as type: String). The list of built-in types is listed in the docs, but it's short enough to repeat here: it's Boolean, Date, Number, String, Array and Object.

In the case where your property could take many different types, you probably want to use the TypeScript 'any'. If you want to be able to initialize it from an attribute, Object is often a good choice--it'll first try to parse it as a JSON string; if that fails, it treats the attribute value as a string.

If you need more complicated deserialization logic, you can write a custom deserializer.

divyanshu-rawat commented 4 years ago

@arthurevans sounds good 👍 , as far as I have concluded polymerhas no native support for any type, we have to createcustom type.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.