ts-inject is a 100% typesafe dependency injection framework for TypeScript projects, designed to enhance code sharing and modularity by ensuring compile-time dependency resolution. This framework leverages the dependency injection design pattern to decouple dependency usage from creation, allowing components to rely on interfaces rather than implementations.
ts-inject brings typesafety to dependency injection, setting it apart from a vast majority of frameworks, like InversifyJS, which operate at runtime and therefore lack this level of typesafety.
While typed-inject also prioritizes typesafety, it lacks several key features that ts-inject offers:
ts-inject navigates around TypeScript's limits on nested types, making it more robust for complex applications.ts-inject enables merging multiple containers, facilitating greater modularity and code reuse.npm install @snap/ts-inject
This quick start guide demonstrates how to define services, register them in a container, and then retrieve them for use.
Define a couple of services. For simplicity, we'll use a Logger service and a Database service, where Database depends on Logger for logging purposes.
class Logger {
log(message: string) {
console.log(`Log: ${message}`);
}
}
class Database {
static dependencies = ["Logger"] as const;
constructor(private logger: Logger) {}
save(record: string) {
this.logger.log(`Saving record: ${record}`);
}
}
With ts-inject, you can set up a container to manage these services using providesValue and providesClass:
import { Container } from "@snap/ts-inject";
const container = Container.providesValue("Logger", new Logger()).providesClass("Database", Database);
const db = container.get("Database");
db.save("user1"); // Log: Saving record: user1
When a service needs custom instantiation logic — such as transformation, conditional setup, or isn't a simple class — use Injectable() with .provides():
import { Container, Injectable } from "@snap/ts-inject";
const container = Container.providesValue("apiUrl", "https://api.example.com").provides(
Injectable("httpClient", ["apiUrl"], (url: string) => createHttpClient(url))
);
providesValue and providesClass cover the vast majority of use cases. Reach for Injectable() only when you need a factory function with custom logic.
ts-inject supports composable containers, allowing you to modularize service registration:
const baseContainer = Container.providesValue("Logger", new Logger());
const appContainer = baseContainer.providesClass("Database", Database);
const db = appContainer.get("Database");
db.save("user2"); // Log: Saving record: user2
You can also bootstrap a container from a plain object with fromObject:
const configContainer = Container.fromObject({ apiUrl: "https://api.example.com", timeout: 5000 });
Containers support appending to array-typed services, useful for plugin systems and extensible pipelines:
const container = Container.providesValue("plugins", [] as Plugin[])
.appendClass("plugins", AuthPlugin)
.appendClass("plugins", LoggingPlugin)
.appendValue("plugins", { name: "inline", run: () => {} });
container.get("plugins"); // [AuthPlugin, LoggingPlugin, { name: "inline", ... }]
dependencies field to enable automatic injection via providesClass.Injectable() to create factory functions with explicit dependency lists. Use this when providesValue/providesClass don't fit your needs.For comprehensive documentation of all ts-inject features and APIs, please refer to the API Reference.
ts-inject is published under MIT license.
ts-inject originated as an internal project at Snap Inc., developed by Weston Fribley. Inspired by the principles of typed-inject, it was designed to address the limitations of existing dependency injection frameworks and improve typesafe dependency resolution in TypeScript. Initially aimed at enhancing CameraKit's codebase, its success led to its adoption across various teams at Snap Inc., and now it has evolved into an open-source project to benefit the wider TypeScript community.