because I get errors in typescript but it works well?

0
function MiDecorador(paramtro1:string){
    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){}
    }

Because I get an error, it means a red line below and according to the error is:

  

Unable to solve signature of decorator when called as an   expression Can not invoke an expression whose type lacks a call   signature Type 'void' has not supported call signatures.

That I get it but the truth that works for me just wanted to know that thanks.

    
asked by DavidIjsud 22.01.2018 в 22:46
source

1 answer

0

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.

    
answered by 22.01.2018 в 23:16