nicojs / typed-inject

Type safe dependency injection for TypeScript
Apache License 2.0
430 stars 23 forks source link

Support for high order functions #51

Open mhsdesign opened 2 years ago

mhsdesign commented 2 years ago

i set up my project using higher-order functions for the dependencies similar to: https://medium.com/geekculture/dependency-injection-in-javascript-2d2e4ad9df49

// application/ports.ts
export interface Logger {
  log(msg: string): void;
}

// application/fooFunction.ts
interface Dependencies {
  logger: Logger;
}

export const makeFooFunction({ logger }: Dependencies ) = (foo: string) => {
   // foo bar
   logger.log("bung" + foo)
}

i was wondering if this higher-order pattern could technically also be implemented / be used for di with this project (if yes maybe i can build it myself but idk)

(fx. by setting makeFooFunction.inject = ['logger'])

mhsdesign commented 2 years ago

uups sorry there is already https://github.com/nicojs/typed-inject#injectorinjectfunctionfn-injectablefunction

(i find this beeing to far down in the docs -> its not teasered like class di at the top)