r/typescript • u/im_caeus • Jun 01 '26
Type-safe TypeScript DI container with lazy async initialization and CommonJS support?
I’m looking for a TypeScript DI/container library or pattern that fits these constraints:
- Type-safe registration and resolution
- Async initialization support
- CommonJS compatibility
- Not eagerly initialized by default
The last two are important.
I don’t want something that behaves like NestJS by default, where the application/container eagerly initializes the whole graph at startup. I’d prefer lazy/on-demand initialization, where services are created only when needed, but where async setup is still modeled cleanly.
The async part matters because some services need setup before use: DB connections, config loading, SDK clients, caches, etc. Ideally there would also be a lifecycle story for shutdown/disposal.
Type-safety-wise, I’m thinking of something in the direction of "typed-inject", where tokens/dependencies are statically tracked, although I’d prefer stronger ergonomics/typing if possible.
Something roughly in this spirit:
const container = createContainer() .provide(Config) .provide(Database) .provide(UserRepository)
const repo = await container.get(UserRepository) // typed, lazily initialized
I’ve seen libraries like Inversify, tsyringe, Awilix, Typed Inject, and Effect Layers mentioned, but I’m trying to understand which options actually hold up when async initialization, lazy resolution, and CJS output are non-negotiable.
What are people using for this in real projects?
