class-transformer icon indicating copy to clipboard operation
class-transformer copied to clipboard

Why is the type evaluated first?

Open JeonSoongu opened this issue 2 years ago • 1 comments

@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.

JeonSoongu avatar May 14 '22 06:05 JeonSoongu

I guess I found 'why' it calls @Type first as below. image 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 :)

JeonSoongu avatar May 14 '22 08:05 JeonSoongu

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.

StenCalDabran avatar Jan 21 '23 01:01 StenCalDabran

Closing as answered. If you have any questions left feel free to comment on this issue. Thank you @StenCalDabran for the answer.

diffy0712 avatar May 08 '24 21:05 diffy0712