solenya-group/solenya

Type decorator clarification?

Closed this issue · 5 comments

In the documentation, I see an example of the @Type() decorator. I understand the purpose of this decorator, to provide a hook to get type information to run time - TypeScript has lots of positive attributes, but it inconveniently doesn't have a feature to bring types along at runtime even if you want them.

export class Tree extends Component
{    
    @Type (() => Tree) trees: Tree[] = []
    ...
}

The confusing thing here though is that I would naïvely expect the code to look more like this:

export class Tree extends Component
{    
    @Type (() => Tree[]) trees: Tree[] = []
    ...
}

... Rather than automagically knowing when to treat the reference as an array rather than a single instance.

I understand this actually arrives from class-transformer, but a few words about it in the documentation would probably help understand it at a first glance.

Good point, and in fact when I used it first the time that was my expectation too. I'll revise the docs.

Here's the new property serialization section:
https://github.com/pickle-ts/pickle#property-serialization

Also released an update to pickle that simplifies serialization, by preserving NaN values via decorating a property with @num

Could still do a better job here more generally explaining class-transformer.

Since this was opened:

  1. Non-undefined non-array properties are auto-decorated using initTransformers
  2. The documentation has been updated.

Closing this issue, as the only improvement at this point would have to come from the typescript language itself automatically decorating properties based on their type.