Buslowicz / twc

TypeScript based, boilerplate-less, Polymer toolbox friendly Polymer Modules
32 stars 1 forks source link

any should become undefined type in javascript not object #137

Open goatandsheep opened 6 years ago

goatandsheep commented 6 years ago

I'm trying to push type number into type Array using a generic component, but because any becomes object, not undefined, it is causing huge errors in the application.

Buslowicz commented 6 years ago

Could you provide an example of that? I am not sure I understand what you described.

Buslowicz commented 6 years ago

@goatandsheep Could you please reexplain what is the issue? I would appreciate some code samples illustrating what goes wrong.

goatandsheep commented 6 years ago

I'll give you a pseudocode example for now, but I hope it illustrates the issue.

child {
  public value: any;
}
parent1 {
  @template(`
    <child value="{{value}}"></child>
  `)
  public value: string;
}
parent2 {
  @template(`
    <child value="{{value}}"></child>
  `)
  public value: number;
}
view {
  @template(`
    <parent1 value="hi"></parent1>
    <parent2 value="2"></parent2>
  `)
}
Buslowicz commented 6 years ago

If I understand correctly, you want a property with mixed type. Sadly, this does not work with Polymer itself, so TWC has no chance to support it. Please familiarize yourself with this polymer docs page. You have to specify the type explicitly, and Polymer by default supports only Boolean, Date, Number, String, Array and Object types, but not a mixture of those.

goatandsheep commented 6 years ago

We've made many components with type: undefined or simply no type field and they've worked extremely well. In fact we're converting working Polymer components to Typescript. In addition, the Typescript manual states:

The any type is a powerful way to work with existing JavaScript, allowing you to gradually opt-in and opt-out of type-checking during compilation. You might expect Object to play a similar role, as it does in other languages. But variables of type Object only allow you to assign any value to them - you can’t call arbitrary methods on them, even ones that actually exist...

Buslowicz commented 6 years ago

Hmm, OK so I had no knowledge of type: undefined being valid. If this is the case, I will make any to become undefined (probably undefined will be the default for any or mixed types like number | string). I will try to fix this issue as soon as I find a moment.

Buslowicz commented 6 years ago

@goatandsheep I studied through that issue. When type is set to non-supported type (null and undefined are unsupported types), serialization and deserialization does not happen. This means using the default attribute type on deserialization (which is a string), and calling .toString() on an object on serialization. The exact same behavior (as literally a fall-through in a switch) has a String type, so if you set the type to be a string, it will work (aside of type hinting in typescript itself, which can be temporarily fixed by casting the type to any). I have pushed the static change to default type, but it would be a breaking change if I release it as is, and twc is not yet ready for a major version update. Instead I will add an option in the config file (still need to make a config file :D) what to use as a default type (might change the default option to String in the major version update). Meanwhile, I released a temporary version so I don't hold you back, it will not be under latest tag on npm, but on temp, so please install it via npm i twc@temp. I will keep this issue open and will close it once the config file with that option is there, so please keep the issue subscribed for updates.