Is it possible to decorate a nested property within a class? Let's explore with an example:
function log(name: string = 'DecoratedProp') {
return function logHandler(target: any, field: any) {
// get the key
Object.defineProperty(target, field, {
get() {
console.info('Accessing:' + name, field)
return this[name];
},
set(value: any): void {
console.warn('Setting:' + name, field)
this[name] = value;
}
})
};
}
class Nested {
@log('Nested') data: string;
}
export class DecoratedModel {
nested: Nested = {
data: 'something',
// decorating here won't work, e.g.
// @log() somethingElse; -> breaks
};
@log() topLevel: string = '33';
}
const deco = new DecoratedModel();
console.log(deco.topLevel); // logs stuff
console.log(deco.nested.data); // doesn't log
Why isn't my nested property showing as decorated?