Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tsyringe

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsyringe - npm Package Compare versions

Comparing version 1.0.0 to 1.0.1

2

package.json
{
"name": "tsyringe",
"version": "1.0.0",
"version": "1.0.1",
"description": "Lightweight dependency injection container for JavaScript/TypeScript",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -5,4 +5,93 @@ [![Travis](https://img.shields.io/travis/Microsoft/tsyringe.svg)](https://travis-ci.org/Microsoft/tsyringe/)

A lightweight dependency injection container for TypeScript/TypeScript
A lightweight dependency injection container for TypeScript/TypeScript for
constructor injection.
## Installation
```sh
npm install --save tsyringe
```
Modify your `tsconfig.json` to include the following settings
```json
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
```
## Usage
### Example without interfaces
Since classes have type information at runtime, we can resolve them without any
extra information. If a particular class isn't registered with the container then
a new instance will be resolved each time.
```TypeScript
// Foo.ts
export class Foo {}
```
```TypeScript
// Bar.ts
import {Foo} from "./Foo";
import {decorators} from "tsyringe";
const {injectable} = decorators;
@injectable()
export class Bar {
constructor(public myFoo: Foo) {}
}
```
```TypeScript
// main.ts
import {container} from "tsyringe";
import {Bar} from "./Bar";
const myBar = container.resolve(Bar);
// myBar.myFoo => An instance of Foo
```
### Example with interfaces
Interfaces don't have type information at runtime, so we need to decorate them
with `@inject(...)` so the container knows how to resolve them.
```TypeScript
// SuperService.ts
export interface SuperService {
// ...
}
```
```TypeScript
// TestService.ts
import {SuperService} from "./SuperService";
export class TestService implements SuperService {
//...
}
```
```TypeScript
// Client.ts
import {decorators} from "tsyringe";
const {injectable, inject} = decorators;
@injectable()
export class Client {
constructor(@inject("SuperService") private service: SuperService) {}
}
```
```TypeScript
// main.ts
import {Client} from "./Client";
import {TestService} from "./TestService";
import {container} from "tsyringe";
container.register({
token: "SuperService",
useClass: TestService
});
const client = container.resolve(Client);
// client's dependencies will have been resolved
```
## Contributing

@@ -9,0 +98,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc