Adding @Alias decorator and allow multiple @Expose decorators #1192
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
Thank you for this amazing library 🚀
It has been discussed several times (sometimes with some confusion): in some scenarios, being able to rename a property only when importing or exporting it can be useful.
For instance, I am reading rows from a file in a format A sent by a third party, then convert it to an object in my TS code. It might be logged or stored in a file in another format B later on.
One of the field is named
part_or_item_number
in the format A, which is then converted topartRef
in my JS object.TLDR
plainToInstance
should accept this object:The property should be referenced as follow in the code:
And
instanceToPlain
should then produce the following:One would quickly suggest to use
@Expose({ name: 'part_or_item_number' })
, however it would also expose the property under the namepart_or_item_number
when usinginstanceToPlain
.Adding the option
toClassOnly: true
is not cutting it either as it would then hide the property when usinginstanceToPlain
.Another suggestion seen in one of the thread linked earlier is to use two
@Expose
:While it's a pretty neat idea, it simply doesn't work in the current state, as the last
@Expose
will always replace the previous ones.This PR introduces two changes:
Alias
decorator to reduce boilerplate in that scenario, using@Expose
behind the scene to reduce duplicationThe code above could be rewritten as the following with the same expected behaviour:
There is also a slight rewriting of the map manipulation code in
MetadataStorage.ts
. I'm fine reverting it but it greatly improved the readability for me.Checklist
Update index.md
)develop
)npm run prettier:check
passesnpm run lint:check
passesFixes
fixes #677, fixes #121, fixes #366