babel-plugin-transform-decorators-legacy icon indicating copy to clipboard operation
babel-plugin-transform-decorators-legacy copied to clipboard

Cannot assign to read only property of object

Open pleerock opened this issue 8 years ago • 10 comments

I have such error: Cannot assign to read only property 'name' of object '#<Category>' when I use non initialized properties decorated with decorators and then trying to assign values to these properties.

import {Entity, PrimaryColumn, Column} from "typeorm";

@Entity()
export class Category {

    @PrimaryColumn("int", { generated: true })
    id;

    @Column("string")
    name;

}

Solution is to do it this way:

import {Entity, PrimaryColumn, Column} from "typeorm";

@Entity()
export class Category {

    @PrimaryColumn("int", { generated: true })
    id = undefined;

    @Column("string")
    name = "";

}

Which is ugly. I guess the reason is that it does not make descriptor writable. Is there any better solution for this problem?

pleerock avatar Jan 27 '17 11:01 pleerock

This seems like an issue with the typeorm decorators, rather than an issue with this module?

loganfsmyth avatar Jan 27 '17 16:01 loganfsmyth

No, I think its not. Simple example of reproduction without typeorm:

function dec(id) {
    return (target, property, descriptor) => console.log("executed", id);
}

export class Category {

    @dec()
    id = 0;

    @dec()
    name;

}

const category = new Category();
category.id = 1;
category.name = "hello";

produces:

/Users/pleerock/www/opensource/typeorm/babel-example/dist/index.js:79
category.name = "hello";
              ^

TypeError: Cannot assign to read only property 'name' of object '#<Category>'
    at Object.<anonymous> (/Users/pleerock/www/opensource/typeorm/babel-example/dist/index.js:79:15)

Note that category.id = 1 worked because id was initialized in the class.

pleerock avatar Jan 27 '17 18:01 pleerock

Would you be able to put together a simple example repo? Dropping that into a repo with .babelrc

"transform-class-properties",
"transform-decorators-legacy",

logs

executed undefined
executed undefined

for me, so something else may be interfering?

loganfsmyth avatar Jan 27 '17 18:01 loganfsmyth

why do you put "transform-class-properties" before "transform-decorators-legacy"?

pleerock avatar Jan 27 '17 18:01 pleerock

Here is my configuration:

{
  "presets": [
    "es2015"
  ],
  "plugins": [
    "transform-decorators-legacy",
    "transform-class-properties"
  ]
}

pleerock avatar Jan 27 '17 18:01 pleerock

You are right, I tried both to make sure and then copy-pasted the wrong one.

I am able to reproduce with es2015. Thanks for the info.

loganfsmyth avatar Jan 27 '17 18:01 loganfsmyth

The problem originates from this check: index.js (LINE 54)

I'm not familiar enough with the code to be sure if it can be safely removed. Any opinion?

hwaterke avatar Feb 03 '17 18:02 hwaterke

Yeah that's definitely the line. That was carried over from Babel 5.x here: https://github.com/babel/babel/blob/5.x/packages/babel/src/transformation/templates/helper-create-decorated-class.js#L15 and the same bug reproduces in Babel 5 too.

loganfsmyth avatar Feb 03 '17 18:02 loganfsmyth

Any solutions?

SPAHI4 avatar Feb 16 '17 22:02 SPAHI4

Is this solved?

chbdetta avatar Dec 05 '17 12:12 chbdetta