Skip to content(if available)orjump to list(if available)

Chibi Izumi: Phased dependency injection for TypeScript

pshirshov

A bit surprised (and delighted) to see this on the front page.

Essentially, this is a greatly simplified port of distage (my library implementing phased DI for Scala).

Most of the job was done by Claude, the primary point was to showcase phased DI for Typescript, which has many annoyances and limitations, especially when it comes to reflection.

My contributions here were

(a) the approach itself: first we turn functions and constructors into runtime-inspectable entities called Functoids, then we trace binding dependencies from requested roots, do conflict resolution and build a DAG of operations, then we produce instances by traversing the graph in topological order.

(b) a bit unconventional approach to Typescript reflection, which is manual but comes with compile-time validation.

There are many benefits of phased approach to DI, one of the most important benefits is that you can have "configurable apps" (think use-flags for your applications) which are sound, free of logical conflicts and validated early (in case of Scala we even do it at compile time).

Also this approach is extremely easy to comprehend and reproduce (even Claude can do it with some guidance and interventions; I've done ports to several other languages, some with LLM assistance, some manually). While most DIs (especially single-phased ones) are hard to comprehend, maintain and port to other languages/runtimes, for this approach you need to have just one concept implemented - Functoid. The DAG-forming logic fits in 200-300 lines of code and would look the same in any language.

traspler

What's the "Non-invasive" metric? How is it less invasive than TSyringe or just as non-invasive as Awilix?

pshirshov

> What's the "Non-invasive" metric?

You can use it with code you can't modify (decorators are just convenience helpers, you can do same through bindings DSL with bit less type safety).

TSyringe depends on reflect-metadata and, if my understanding is correct, forces you to use its decorators.

The comparison table is completely subjective and made with just several glances at the readmes of the mentioned libraries. The point was to showcase phased DI for Typescript.