-
Notifications
You must be signed in to change notification settings - Fork 263
Issues with readonly and enumreable #148
Comments
There are some known issues and non-standard ways that TS compiles decorators unfortunately.
I haven't wanted to spend a bunch of effort on working around them because the decorator proposal in stage-2 changed the spec in very major ways that are incompatible and neither Babel nor TypeScript have implemented it yet. That said, I'm not sure if that's related to your specific issues or not.
If you're using the Perhaps you can elaborate? Sorry you're having issues! |
Sorry, we can close this. I got my answer from the official Typescript repo. |
@JonWallsten oh interesting. Thanks for following up! |
@JonWallsten just fyi, I've found a way to decorate the instance property: /**
* @enumerable decorator that sets the enumerable property of a class field to false.
* @param value true|false
*/
const nonenumerable = (target: any, propertyKey: string) => {
let descriptor = Object.getOwnPropertyDescriptor(target, propertyKey) || {};
if (descriptor.enumerable !== false) {
Object.defineProperty(target, propertyKey, {
enumerable: false,
set(value: any) {
Object.defineProperty(this, propertyKey, {
enumerable: false,
writable: true,
value
});
}
})
}
}
class Employee {
@nonenumerable
public id: number;
public name: string;
constructor() {
this.id = 1;
console.log("this.id | Expected: 1 | Actual: ", this.id);
}
}
var user = new Employee();
user.id = 2;
user.name = 'John Doe';
console.log("user.id | Expected: 2 | Actual: ", user.id);
console.log("Object.keys(user).length | Expected: 1 | Actual: ", Object.keys(user).length);
console.log("Object.keys(user): ", Object.keys(user)); |
I'm currently testing these decorators in a typescript boilerplate project, but I've run into some issues. I might totally miss understand how they work, in that case please enlighten me.
When using read-only and nonenumerable on my props I can't get any values at all to stick.
I also get nog warnings about the prop being read only when trying to set it.
Consider the following code:
Test
Output
The text was updated successfully, but these errors were encountered: