Class-transformer: plainToClass 2 overloads with same parameters confuse the IDE

Created on 1 Jan 2018  路  5Comments  路  Source: typestack/class-transformer

I am using Visual Studio Code and angular 4 (typescript 2)

When using plainToClass like this:
var res = plainToClass(Models.Model, modelData)
The IDE thinks the return value is Models.Model[], it will not compile as I am using it as a singular Models.Model.
modelArray.push(res)

The workaround I found for this issue is using it like this:
plainToClass<Models.Model,object>(Models.Model, modelData)

This way I let the compiler know the V is a singular and not an Array and catch the right overload.

It just makes this code almost twice the size and not as nice to look at as you intended.

core duplicate fix

Most helpful comment

Use:
plainToClass(Models.Model, modelData as Object)
plainToClass(Models.Model, modelData as Object[])

to force proper signature detection. A little bit nicer syntax.

All 5 comments

Use:
plainToClass(Models.Model, modelData as Object)
plainToClass(Models.Model, modelData as Object[])

to force proper signature detection. A little bit nicer syntax.

@sebastian-zarzycki-es thanks for this, we should look at adding this to the main README.md

Thanks for the report, we can fix this probably with the tuning of the function overload declarations.

Duplicate of #30. This is fixed.

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings