My current coding project involves the usage of typescript
decorators in the following way:
function logParameter(target: any, key : string, index : number) {
var metadataKey = `__log_${key}_parameters`;
console.log(target);
console.log(metadataKey === "__log_move_parameters");
console.log(target[metadataKey]); // always undefined
}
class Horse{
public __log_move_parameters: number[];
move(@logParameter distanceInMeters = 45) {
console.log("Galloping...");
}
}
Horse.prototype.__log_move_parameters = [];
let tom: Horse = new Horse();
tom.move(34);
It's interesting to note that despite having __log_move_parameters
in the target
, when I check for target[metadataKey]
, it is always undefined
.
To explore further, you can visit the playground