class-transformer
class-transformer copied to clipboard
Why is the type evaluated first?
@NoNameProvided Hi, i would like to ask you sth.
I'm using c'lass-transformer' in my work. And i found it gives same results in both case, when i mislocate.
@Type()
@Transform()
@Transform()
@Type()
property
The output is all the same. It runs as if i write my code like 'case 1'
Why does always read typemetadata first, regardless of locating? Is there any pre-defined logic to run '@Type()' first? I would like to know the reason 'WHY' it runs first or 'it looks like run first'.
I look forward to hearing from you. Thank you.
I guess I found 'why' it calls @Type first as below.
But still i'd like to know HOW does it work.
If my assumption is wrong, and there's another reason, please let me know HOW and WHY :)
Typescript decorators are executed during class definition, i.e. when the file with the class definition is first evaluated. Both the @Transform
and the @Type
decorator don't do much immediately, at that time they only add some metadata to a singleton of the MetadataStorage
class.
When you then actually execute the trasformation by calling one of the transformation methods, e.g. plainToClass
, it will look up the stored metadata, first the provided type information, then a transformation if provided. Hence the order of the decorators does not matter at all.
Closing as answered. If you have any questions left feel free to comment on this issue. Thank you @StenCalDabran for the answer.