I have a decorator that casts a string number to a javascript number.
Example: "87"
-> 87
.
The code is quite simple:
function digit(target: any, key: string) {
// property value
var _val = this[key];
// property getter
var getter = () => _val;
// property setter
var setter = (newVal) => _val = parseInt(newVal)
// Create new property with getter and setter
Object.defineProperty(target, key, {
get: getter,
set: setter,
enumerable: true,
configurable: true,
});
}
class User {
@digit
public id: number;
public isActive: boolean;
constructor(instanceData) {
this.id = instanceData.id;
this.isActive = instanceData.isActive;
}
}
let user = new User({
id: '712',
isActive: '1'
})
console.log([user.id]) // [ 712 ] as expected
console.log(user) // Person { isActive: '1' }
Why doesn't the id field appear in the second console.log and how can I make it appear ? I can access it but it's hidden in the console.
Thanks !