Open pleerock opened 7 years ago
This seems like an issue with the typeorm
decorators, rather than an issue with this module?
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.
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?
why do you put "transform-class-properties"
before "transform-decorators-legacy"
?
Here is my configuration:
{
"presets": [
"es2015"
],
"plugins": [
"transform-decorators-legacy",
"transform-class-properties"
]
}
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.
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?
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.
Any solutions?
Is this solved?
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.Solution is to do it this way:
Which is ugly. I guess the reason is that it does not make descriptor writable. Is there any better solution for this problem?