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

typedi

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typedi

Dependancy injection for Typescript

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
260K
increased by2.69%
Maintainers
1
Weekly downloads
 
Created

What is typedi?

TypeDI is a dependency injection tool for TypeScript and JavaScript. It allows you to manage the lifecycle and dependencies of your services and components in a clean and efficient way.

What are typedi's main functionalities?

Service Decorator

The `@Service` decorator is used to mark a class as a service that can be injected. The `Container.get` method is then used to retrieve an instance of the service.

```typescript
import { Service } from 'typedi';

@Service()
class ExampleService {
  sayHello() {
    return 'Hello, World!';
  }
}

const exampleService = Container.get(ExampleService);
console.log(exampleService.sayHello()); // Output: Hello, World!
```

Inject Decorator

The `@Inject` decorator is used to inject a dependency into a class. In this example, `DependencyService` is injected into `MainService`.

```typescript
import { Service, Inject } from 'typedi';

@Service()
class DependencyService {
  getValue() {
    return 'Dependency Value';
  }
}

@Service()
class MainService {
  constructor(@Inject(() => DependencyService) private dependencyService: DependencyService) {}

  getValue() {
    return this.dependencyService.getValue();
  }
}

const mainService = Container.get(MainService);
console.log(mainService.getValue()); // Output: Dependency Value
```

Container

The `Container` class is used to manage the lifecycle of services. You can manually set and get instances of services using `Container.set` and `Container.get`.

```typescript
import { Container } from 'typedi';

class ExampleService {
  sayHello() {
    return 'Hello, World!';
  }
}

Container.set(ExampleService, new ExampleService());
const exampleService = Container.get(ExampleService);
console.log(exampleService.sayHello()); // Output: Hello, World!
```

Other packages similar to typedi

FAQs

Package last updated on 09 Oct 2015

Did you know?

Socket

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.

Install

Related posts

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