Home > Software engineering >  TS start complaining about KEY added by decorator
TS start complaining about KEY added by decorator

Time:08-14

I did create a decorater function called KeyAdder also a class named User. I decorated the class with KeyAdder and i expected to have a key on my instance object. But when i tried to access that property i got an error: Property 'version' does not exist on type 'User'.

Here is code:

function KeyAdder(target: Function) {
    target.prototype.key = 'value';
}

@KeyAdder
class User {
    constructor(
        public username: string,
    ) {}
}


const u = new User("MORA");
console.log(u.key); //this line is problematic 

How can i inform typescript about existence of key?

CodePudding user response:

This feature does not yet exist in TypeScript. However, there is an open GitHub feature request that has been around since 2015 which has lots of support, though it seems that the TypeScript team won't implement or consider it until the JS decorator proposal is finished and supported by browsers.

  • Related