GillianPerard / typescript-json-serializer

A typescript library to deserialize json into typescript classes and serialize classes into json.
MIT License
211 stars 29 forks source link

TypeError: Cannot read property 'toString' of undefined #99

Closed omarbelkhodja closed 4 years ago

omarbelkhodja commented 4 years ago

After adding an import to the following class, I'm facing an error:

@Serializable()
export class MyClass1 {
    @JsonProperty() id: string = '1';
}
Uncaught (in promise) TypeError: Cannot read property 'toString' of undefined
    at getJsonPropertyValue (index.js?c608:284)
    at eval (index.js?c608:85)
    at Object.decorateElement (myclass1.ts:23)
    at Object.eval (myclass1.ts:23)
    at Array.forEach (<anonymous>)
    at Object.decorateClass (myclass1.ts:23)
    at _decorate (myclass1.ts:21)
    at eval (myclass1.ts?eef3:6)
    at Module../src/libraries/my-lib/myclass1.ts (0.js:1959)
    at __webpack_require__ (app.js:833)
GillianPerard commented 4 years ago

Hi, could you tell me what kind of project you are building: React, Angular, NodeJS, another one?

omarbelkhodja commented 4 years ago

I'm building pwa-webpack-starter-kit.

GillianPerard commented 4 years ago

Ok.

Did you add "emitDecoratorMetadata": true and "experimentalDecorators": true inside your tsconfig.json file?

omarbelkhodja commented 4 years ago

Yes, they are already there. I think there is a specific issue related to babel/webpack compiling of TS.

GillianPerard commented 4 years ago

I know that babel does not support emitDecoratorMetadata, but webpack should work, let me inspect.

omarbelkhodja commented 4 years ago

Did you succeed to reproduce the problem ?

GillianPerard commented 4 years ago

I'm currently working on another project for the moment, I'll give you some feedbacks when I have information.

omarbelkhodja commented 4 years ago

My current debug results: the problem happens because only target is defined

function JsonProperty(args) {
    return function (target, key, index)

decorators[i] is a reference to JsonProperty

        decorateElement: function decorateElement(element, placements) {
            var extras = [];
            var finishers = [];
            for (var decorators = element.decorators, i = decorators.length - 1; i >= 0; i--) {
                var keys = placements[element.placement];
                keys.splice(keys.indexOf(element.key), 1);
                var elementObject = this.fromElementDescriptor(element);
                var elementFinisherExtras = this.toElementFinisherExtras( (0, decorators[i])(elementObject) || elementObject);
omarbelkhodja commented 4 years ago

Adding legacy: true in Babel configuration fixes the issue (but creates a failure somewhere else). The issue is for sure not in the typescript-json-serializer package itself. I think we can close this issue.

['@babel/proposal-decorators', {
      legacy: true
}]
GillianPerard commented 4 years ago

Thank you for your investigation, yes it seems 😕

saileshbro commented 3 years ago

I am getting the same issue. I am using it in cloud functions

saileshbro commented 3 years ago

I am not sure what happened, i was using JSdoc comments, and it was fixed after i removed the comments.

GillianPerard commented 3 years ago

Hi, could you open a new issue please and follow the instructions to describe the problem?