Here is an interface that I am working with:
interface IFactory<T> extends Function {
(...args: any[]): (((...args: any[]) => T)|T);
}
After implementing the following code snippet, an error occurred:
ts] Type '((...args: any[]) => IKatana) | IKatana' is not assignable to type 'IKatana'. Type '(...args: any[]) => IKatana' is not assignable to type 'IKatana'. Property 'hit' is missing in type '(...args: any[]) => IKatana'. (property) NinjaWithUserDefinedFactory._katana: IKatana
@injectable()
class NinjaWithUserDefinedFactory implements INinja {
private _katana: IKatana;
private _shuriken: IShuriken;
public constructor(
@inject("IFactory<IKatana>") katanaFactory: IFactory<IKatana>,
@inject("IShuriken") shuriken: IShuriken
) {
this._katana = katanaFactory(); // error!
this._shuriken = shuriken;
}
public fight() { return this._katana.hit(); };
public sneak() { return this._shuriken.throw(); };
}
There have been instances where the factories are invoked multiple times due to configuration, leading to more difficulties:
Cannot invoke an expression whose type lacks a call signature.
class Engine {
constructor(type: string, cc: number) {}
}
let engineFactory: IFactory<Engine> = (type: string) => (cc: number) => {
return new Engine(type, cc);
};
let dieselEngine = engineFactory("diesel");
let dieselEngine300cc = dieselEngine(300); // error!
let dieselEngine320cc = dieselEngine(320); // error!
Any suggestions on how to tackle this issue?
Update
There are discussions within the Typescript team regarding Variadic Kinds, which may offer a solution to this problem.