Reputation: 3023
Given the following:
decorator.ts
export function logStuff(target: Object, key: string | symbol, descriptor: TypedPropertyDescriptor<any>) {
return {
value: function (...args: any[]) {
args.push("Another argument pushed");
descriptor.value.apply(target, args);
}
};
}
Shell.ts
// Removed other imports for brevity
import logStuff = require("utils/log-decorator");
class Shell extends AnotherClass {
constructor() {
super();
this.fooMethod("arg1");
}
@logStuff
private fooMethod(arg1: string, arg2?: string) {
console.log(`Arguments from original function: ${JSON.stringify(arguments)}`);
}
}
export = Shell;
I get this message (shortened the file path for brevity):
Unable to resolve signature of method decorator when called as an expression. Cannot invoke an expression whose type lacks a call signature. Type 'typeof "/utils/log-decorator"' has no compatible call signatures
However, if I move the function to the top of Shell.ts, it compiles without errors. Any suggestions on how to handle this?
Upvotes: 1
Views: 2178
Reputation: 40554
Your logStuff
is available as an exported member of the module. So you have to access it like:
import logStuffModule = require("utils/log-decorator");
//...
@logStuffModule.logStuff
private fooMethod(arg1: string, arg2?: string) { ... }
Or use ES6-style imports:
import { logStuff } from "utils/log-decorator";
// ...
@logStuff
private fooMethod(arg1: string, arg2?: string) { ... }
// decorator.ts
export = function logStuff() {}
// Shell.ts
import logStuff = require("utils/log-decorator");
// ...
@logStuff
private fooMethod(arg1: string, arg2?: string) { ... }
Upvotes: 3