Introduction
@favy/di
is a lightweight, TypeScript-powered tool that makes dependency injection effortless. It lets you build modular, composable functionality with zero boilerplate—just write functions and focus on your logic while it handles the rest.
Ideal for building streamlined, testable, and future-proof applications.
Motivation
Most dependency injection libraries are overly complex, requiring excessive boilerplate. With @favy/di
, managing dependencies is as simple as writing a function.
Powered by TypeScript, it automatically infers dependencies
based on what your function uses—no explicit declarations needed. Dependencies are passed through an ambient context
, so you can focus on business logic instead of infrastructure.
Each module is just a function
that takes an object, making it flexible, intuitive, and easy
to use anywhere. Build clean, testable, and maintainable applications without unnecessary overhead.
Install:
Setting Up a Simple Module
A module is defined using the Module function:
Defining Parameters
You can define modules that accept parameters:
Composing Modules
Modules can be composed to depend on other modules dynamically. Here’s an example:
Dependency Injection
Modules support dynamic dependency injection, allowing for highly composable functionality:
Deep Dependency Replacement in Module Architecture
This example demonstrates how to replace dependencies at any depth level within a modular application structure. The implementation shows the flexibility of replacing deep-level dependencies without modifying the higher-level modules.
Module Structure
The application consists of five hierarchical levels:
- Logger (Base level)
- Config
- UserRepository
- UserService
- App (Top level)
Demonstrated Capability
The implementation shows how to:
- Replace a deep-level dependency (UserRepository) while keeping all other modules intact
- Maintain the full functionality of the application while using the replaced dependency
- Preserve type safety when replacing modules
The example specifically demonstrates replacing the UserRepository with a test implementation and the Logger with a no-op version, showing that dependencies can be replaced regardless of their position in the module hierarchy.
The diagram visualizes both the standard module connections and how the system works with replaced dependencies, while the code shows the practical implementation.
Code
Discover More
Explore our comprehensive documentation to discover the full power of the module system, including advanced features, real-world examples, and best practices for building scalable applications.