tsyringe icon indicating copy to clipboard operation
tsyringe copied to clipboard

Is is possible to resolve function parameters?

Open user753 opened this issue 5 years ago • 8 comments

I want to do something like this

class Foo {}
class Bar {}
function fooBar(foo: Foo, bar: Bar){
    return () => {
       // can use foo and bar instances
    }
}
const fb = container.resolve(fooBar)

I can do this, but I don't like [Foo, Bar] part.

function resolveFunction<T extends any[], R>(fn: (...x: T) => R,
  deps: {[K in keyof T]: InjectionToken<T[K]>}): R {
    const r = deps.map(container.resolve)
    return fn(...(r as any))
}

const x = resolveFunction((foo: Foo, bar: Bar) => ..., [Foo, Bar])

Is is possible to do something better?

user753 avatar Jul 16 '19 12:07 user753

Sounds like you want to retrieve parameter-less a handle to a new function which you can execute as if it were your function fooBar(...)?

Something like:

@injectable()
function fooBar(foo: Foo, bar: Bar) {
   // do stuff with foo and bar
}

const injected = container.resolve(fooBar);

injected(); // Does stuff

Currently the answer is no, the @injectable decorator (and others) does not support this. However, I think this is totally possible and would be willing to accept a PR for such a feature =]

Xapphire13 avatar Jul 16 '19 23:07 Xapphire13

Unfortunately, functions cannot be decorated in TypeScript currently. I went to implement this, but ran quickly into that wall.

MeltingMosaic avatar Jul 26 '19 05:07 MeltingMosaic

@MeltingMosaic Could class static method be used?

class Factory {
  @injectable
  static fooBar(foo: Foo, bar: Bar) {
     // do stuff with foo and bar
  }
}

user753 avatar Jul 26 '19 07:07 user753

~~It's possible. I've done it in my own fork of a di container. In https://github.com/nebez/good-injector-async you can find examples of method invocation. See here for a test/example: https://github.com/nebez/good-injector-async/commit/b6ae71d4e3e5c730f6736f3fcea3550d3dbf190d#diff-9d05ba8fe85bfcbf71d6b2ab8a6e8b06R13-R16~~

~~Willing to take this on if nobody else has the appetite for it.~~

edit: ah, misunderstood. This is for functions not instance methods.

ghost avatar Dec 13 '19 19:12 ghost

Would a non-decorator solution to this be acceptable in a PR?

hartror avatar May 15 '20 08:05 hartror

@hartror, could you provide some pseudocode that might give an example of how you would implement this?

MeltingMosaic avatar Jun 03 '20 21:06 MeltingMosaic

I think it very important. Would you have solution for this?

namnhp-2137 avatar Jul 01 '20 03:07 namnhp-2137

Still not possible?

anton-zen avatar Oct 17 '23 06:10 anton-zen