Quickstart
Dipend is a lightweight dependency injection container for TypeScript that supports interface-based registration, a feature no other TypeScript DI library offers natively.
Follow these simple steps to get started:
Prerequisites
Section titled “Prerequisites”Before getting started, make sure you have:
- Node.js ≥ 18.x
- TypeScript installed (tsc)
- ts-node (optional, to run .ts files without compiling)
Setup from Scratch
Section titled “Setup from Scratch”Follow these steps to create a new project using Dipend:
# 1. Create a new directory and initialize the projectmkdir my-app && cd my-appnpm init -y
# 2. Install TypeScript (as a dev dependency)npm install --save-dev typescript
# 3. Initialize TypeScript configurationnpx tsc --init
# 4. (Optional) Install ts-node to run TypeScript files directlynpm install --save-dev ts-node
# 5. Initialize Dipendnpx dipend init
# 6. Install project dependenciesnpm install
This will:
- Add
dipend
to yourpackage.json
- Update your
tsconfig.json
with the required options
Optional: If you want to visualize your dependency graph in a browser, install dipend-graph:
npm install dipend-graph
When You Run npx dipend init
:
Section titled “When You Run npx dipend init:”Dipend automatically updates your package.json
to ensure the TypeScript build process is correctly configured.
If your project already has a build script like:
{ "scripts": { "build": "tsc" }}
It will automatically update it to:
{ "scripts": { "build": "dipend tsc" }}
If no script exists, it will NOT add one for you.
By running dipend tsc
instead of plain tsc
, Dipend is able to:
- Analyze and map interface-to-class relationships
- Generate metadata required for dependency resolution
Define Your Interfaces and Classes
Section titled “Define Your Interfaces and Classes”export interface ILogger { info(message: string): void;}
export class ConsoleLogger implements ILogger { info(message: string) { console.log(`[INFO]: ${message}`); }}
Create a Class with Dependencies
Section titled “Create a Class with Dependencies”import { ILogger } from "./logger";
export class Greeter { constructor(private logger: ILogger) {}
greet(name: string): string { const message = `Hello, ${name}!`; this.logger.info(message); return message; }}
Register and Resolve Dependencies
Section titled “Register and Resolve Dependencies”import { DependencyContainer } from "dipend";import { ILogger, ConsoleLogger } from "./logger";import { Greeter } from "./greeter";
const dependencyContainer = new DependencyContainer();
dependencyContainer.addSingleton<ILogger, ConsoleLogger>();dependencyContainer.addTransient<Greeter>();
dependencyContainer.buildSingletons();
const greeter = dependencyContainer.getDependency<Greeter>();console.log(greeter.greet("World"));
With Graph Visualization (Optional):
import { DependencyContainer } from "dipend";import { DipendGraphServer } from "dipend-graph";import { ILogger, ConsoleLogger } from "./logger";import { Greeter } from "./greeter";
const dependencyContainer = new DependencyContainer();
dependencyContainer.addSingleton<ILogger, ConsoleLogger>();dependencyContainer.addTransient<Greeter>();
dependencyContainer.buildSingletons();
const dipendGraphServer = new DipendGraphServer(dependencyContainer);dipendGraphServer.start();
const greeter = dependencyContainer.getDependency<Greeter>();console.log(greeter.greet("World"));
Add Dev Script
Section titled “Add Dev Script”{ "scripts": { "dev": "dipend ts-node ./index.ts" }}
Run Your Application
Section titled “Run Your Application”npm run dev
That’s It!
Section titled “That’s It!”You’re now using fully typed, interface-based dependency injection in TypeScript, with no extra boilerplate or custom tokens.