explaingit

microsoft/tsyringe

5,947TypeScriptAudience · developerComplexity · 3/5LicenseSetup · moderate

TLDR

TSyringe is a lightweight dependency injection library from Microsoft for TypeScript that uses decorators to automatically wire together class dependencies through a central container at runtime.

Mindmap

mindmap
  root((TSyringe))
    What it does
      Dependency injection
      Auto-wire classes
      Central container
    Decorators
      injectable
      singleton
      inject
    Container features
      Register class or value
      Child containers
      Scoped lifetimes
    Setup
      tsconfig changes
      Reflect polyfill
      npm install
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Replace manual class instantiation in a TypeScript app with a container that automatically creates and injects dependencies

USE CASE 2

Make TypeScript services easier to unit test by swapping real implementations for mocks in the container

USE CASE 3

Enforce singleton instances of shared services like a database connection so only one instance ever exists

USE CASE 4

Use child containers to scope dependencies to a specific request or operation lifecycle in a server app

Tech stack

TypeScriptJavaScriptNode.js

Getting it running

Difficulty · moderate Time to first run · 30min

Requires enabling experimentalDecorators and emitDecoratorMetadata in tsconfig.json plus a Reflect polyfill, neither is on by default in TypeScript.

MIT license, use freely for any purpose including commercial apps, with no restrictions beyond keeping the copyright notice.

In plain English

TSyringe is a lightweight library from Microsoft that handles dependency injection for TypeScript and JavaScript applications. Dependency injection is a pattern where a class declares what other objects it needs, and a central system (called a container) creates and supplies those objects automatically rather than having the class create them itself. This makes it easier to swap out components during testing or to change how the application is wired together without touching lots of files. The library works primarily through decorators, which are special annotations you attach to a class or its constructor parameters in TypeScript. For example, marking a class with the @injectable decorator tells TSyringe that it can be resolved by the container, meaning the container will create an instance of it and fill in its dependencies. A @singleton decorator does the same thing but ensures only one instance ever exists and is reused each time something asks for it. The container itself is a registry. You can register a class, a factory function, or a plain value under a named token, and then anywhere in your application you resolve that token to get back the object you registered. Child containers are supported for cases where you want a separate scope, and scoped lifetimes are available for finer control over how long instances live. Installing the library requires npm or yarn, and your TypeScript configuration must enable experimental decorators and decorator metadata, which are not on by default. A polyfill for the Reflect API is also required. The README includes working code examples for common scenarios including injection with and without interfaces, singleton registration, and injecting plain values by name. The project is hosted by Microsoft and released under the MIT license.

Copy-paste prompts

Prompt 1
Show me how to set up TSyringe in a TypeScript project including the tsconfig changes needed for decorators, then write a simple service with constructor injection
Prompt 2
How do I register a singleton database connection in TSyringe and inject it into multiple services without creating duplicate instances?
Prompt 3
Write a unit test using TSyringe where I register a mock HTTP client in the container and inject it into a service under test
Prompt 4
How do I use TSyringe child containers to scope dependencies per HTTP request in an Express.js app?
Prompt 5
What is the difference between @singleton and @injectable in TSyringe, and when should I use each decorator?
Open on GitHub → Explain another repo

← microsoft on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.