/ WhatILearned

Cannot Set a Default Boolean Property in Polymer to any value aside from false

After some discussion in the Polymer Slack Channel, it seems to confirm something that I also came across. It's the fact that when you try to initialize a boolean property (by itself) it must have a value of false.

Here's a typical example:

Polymer({
  is: 'my-element-with-boolean',
  properties: {
       booleanProperty1: Boolean
       booleanProperty2: {
           type: Boolean,
           value:false
       }
  }
 })

Note that for booleanProperty1 there is technically no value attached to it. So it's "null" until it has been set to something.

You might think that for booleanProperty2 you would be able to set the default value to something other than false but apparently boolean properties in Polymer 1 are considered more to be options.

I have luckily been creating deep objects and setting the values of the (deep) members within the objects without any issues (but that's because it's considered part of an Object type and not a boolean).

Polymer({
  is: 'my-element-with-object',
  properties: {
       deepObjectProperty: {
           type: Object,
           value: {  member1: 'This works because Polymer treats this as a default object value',
                      member2: true
            }
       }
  }
 })