Within my coding project, I have implemented a decorator that alters a class by adding additional methods to it, specifically in the class A
. However, when utilizing an instance of this class, the added methods do not show up in the autocomplete feature. Additionally, compiler errors arise due to these properties not being recognized.
I am aware that decorators are incapable of changing the type of a class, as this functionality is not currently supported. Utilizing mixins is also not preferable in this scenario, as the code will eventually be packaged into a library for use by others. It would be more straightforward if users could simply employ the @
notation.
For instance:
function TypeChangeDecorator(constructor: any) {
const keys = Reflect.getMetadata(SOME_KEY_SYMBOL, TypeB);
return class extends constructor {
private someClass: TypeA;
constructor(...args: any[]) {
super(...args);
for(key in keys) {
this[`set${key}`] = () => {} // some method
}
}
someMethod() {
// implementation here
}
}
}
@TypeChangeDecorator
class A {}
Although I understand that it may not be feasible, I am seeking a way to access the methods someMethod
and set${key}
. Ideally, I would like to avoid compiler errors when attempting to access these methods and have them readily available in autocomplete.
PS: While current information suggests that this feature is not yet supported, I am open to any insights or suggestions regarding this matter.