Classroom decorators , have a return void
(empty) or compatible with the class, in this case compatible with Computadoras
, if any parameter is returned, which does not is the case therefore adding :void
, should be resolved.
function MiDecorador(paramtro1:string): void{
console.log(paramtro1);
alert("Simplemente un decorador es una funcion que se aplica a una clase xD");
}
@MiDecorador("Mostrar esto ok")
class Computadoras{
constructor(public nombre:string){}
}
Another option could be more correct, apparently. (which in the end you also end up referencing a void output):
function MiDecorador(paramtro1:string){
return funtion(target: Function){
target.prototype.mensaje = function(): void {
console.log(paramtro1);
alert("Simplemente un decorador es una funcion que se aplica a una
clase xD");
}
}
}
To which you would access by doing:
let computadoras = New computadoras("nombre")
computadoras.mensaje() // La consola te dirá "Mostrar eso ok"
This is better, because:
The class decorator is applied to the constructor of the class and can be
use to observe, modify or replace a class definition.
If the class decorator returns a value, it will replace the declaration
class with the constructor function provided.