Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What about any property type in polymer ? #5597

Open
divyanshu-rawat opened this issue Oct 15, 2019 · 5 comments
Open

What about any property type in polymer ? #5597

divyanshu-rawat opened this issue Oct 15, 2019 · 5 comments
Labels

Comments

@divyanshu-rawat
Copy link

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 user declared 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 any property type in polymer ?

  class XCustom extends Polymer.Element {
    static get properties() {
      return {
        user: String,
      }
    }
  }
@jsilvermist
Copy link

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

@divyanshu-rawat
Copy link
Author

divyanshu-rawat commented Oct 17, 2019

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

@arthurevans
Copy link

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
Copy link
Author

divyanshu-rawat commented Oct 18, 2019

@arthurevans sounds good 👍 , as far as I have concluded polymer has no native support for any type, we have to create custom type.

@stale
Copy link

stale bot commented Oct 17, 2020

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.

@stale stale bot added the wontfix label Oct 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants