I am trying to make a decorator that can:
- Take a parameter
- Store the method it is above as a callable function
- Map the callable function with the parameter the decorator takes.
Here is what I tried:
class Foo {
static cachedMethods: Map<string, (arg: string) => void> = new Map();
static decorator = (decoratorArg: string) => (
target: Object,
propertyKey: string,
descriptor: TypedPropertyDescriptor<any>
): any => Foo.cachedMethods.set(decoratorArg, target as (arg: string) => void);
}
class Bar {
@Foo.decorator("some data")
method1(arg: string) {
console.log(arg);
}
@Foo.decorator("some other data")
method2(arg: string) {
console.log(arg);
}
}
let func: Function = Foo.cachedMethods.get("some data")!;
func("hello world");
However, it says that func is not a function. It is storing it as the Bar class. How can I store the method as a function?