![Oracle Drags Its Feet in the JavaScript Trademark Dispute](https://cdn.sanity.io/images/cgdhsj6q/production/919c3b22c24f93884c548d60cbb338e819ff2435-1024x1024.webp?w=400&fit=max&auto=format)
Security News
Oracle Drags Its Feet in the JavaScript Trademark Dispute
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
@snap/ts-inject
Advanced tools
100% typesafe dependency injection framework for TypeScript projects
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.
// Logger service definition
class Logger {
log(message: string) {
console.log(`Log: ${message}`);
}
}
// Database service depends on Logger
class Database {
constructor(private logger: Logger) {}
save(record: string) {
this.logger.log(`Saving record: ${record}`);
// Assume record saving logic here
}
}
With ts-inject
, you can easily set up a container to manage these services:
import { Container, Injectable } from "@snap/ts-inject";
// Define Injectable factory functions for services
const loggerFactory = Injectable("Logger", () => new Logger());
const databaseFactory = Injectable("Database", ["Logger"] as const, (logger: Logger) => new Database(logger));
// Create a container and register services
const container = Container.provides(loggerFactory).provides(databaseFactory);
// Now, retrieve the Database service from the container
const db = container.get("Database");
db.save("user1"); // Log: Saving record: user1
ts-inject
supports composable containers, allowing you to modularize service registration:
const baseContainer = Container.provides(loggerFactory);
const appContainer = Container.provides(baseContainer).provide(databaseFactory);
const db: Database = appContainer.get("Database");
db.save("user2"); // Log: Saving record: user2
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.
FAQs
100% typesafe dependency injection framework for TypeScript projects
The npm package @snap/ts-inject receives a total of 1,260 weekly downloads. As such, @snap/ts-inject popularity was classified as popular.
We found that @snap/ts-inject demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Oracle seeks to dismiss fraud claims in the JavaScript trademark dispute, delaying the case and avoiding questions about its right to the name.
Security News
The Linux Foundation is warning open source developers that compliance with global sanctions is mandatory, highlighting legal risks and restrictions on contributions.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.