Inject is a lightweight dependency injection container for JS & TS
pnpm install @benstack/inject
abstract class Globals {
// ...
}
class MyGlobals extends Globals {
// ...
}
const env = new InjectionKey<string>;
class MyClass {
env = inject(env)
globals = inject(Globals)
}
const container = new Container;
container.registerSingleton(Globals, MyGlobals)
container.registerTransient(MyClass);
container.registerValue(env, "development")
const myclass = container.resolve(MyClass)
assertEquals(myclass.env, "development") // true
assert(myclass.globals instanceof Globals) // true
Made with ๐
Published under MIT License.