# @alterior/di

> Flexible reflection-based dependency injection

Latest version **3.16.4** (published 2026-07-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @alterior/di
pnpm add @alterior/di
yarn add @alterior/di
bun add @alterior/di
```

## Health

**Score 60/100 (C)** — status: active.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.16.4 |
| Published | 2026-07-27 |
| First published | 2018-09-07 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=10 |
| Dependencies | 2 |
| Unpacked size | 464.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 16 |
| Author | The Alterior Project |
| Maintainers | rezonant |
| Keywords | DI, dependency, injection, dependency injection, injector |

## Links

- npm: https://www.npmjs.com/package/@alterior/di
- Repository: https://github.com/alterior-mvc/alterior
- Homepage: https://github.com/alterior-mvc/alterior/blob/main/packages/di/README.md
- Issues: https://github.com/alterior-mvc/alterior/issues
- npm.io page: https://npm.io/package/@alterior/di

## Dependencies (2)

- [@alterior/common](https://npm.io/package/@alterior/common.md) ^3.16.4
- [@alterior/annotations](https://npm.io/package/@alterior/annotations.md) ^3.16.4

## Alternatives

- [memory-cache](https://npm.io/package/memory-cache.md) — 795.0K weekly downloads
- [@httptoolkit/proxy-agent](https://npm.io/package/@httptoolkit/proxy-agent.md) — 11.2K weekly downloads
- [express-cache-controller](https://npm.io/package/express-cache-controller.md) — 5.3K weekly downloads
- [http-cache-middleware](https://npm.io/package/http-cache-middleware.md) — 4.5K weekly downloads
- [cache2](https://npm.io/package/cache2.md) — 1.5K weekly downloads

## Recent versions

- 3.16.4 (latest) — 2026-07-27
- 3.16.3 — 2026-07-22
- 3.16.2 — 2026-05-21
- 3.16.1 — 2026-05-21
- 3.16.0 — 2026-05-14
- 3.15.0 — 2026-02-22
- 3.14.3 — 2026-02-22
- 3.14.2 — 2026-02-22
- 3.14.1 — 2026-02-13
- 3.14.0 — 2026-01-25
- 3.13.4 — 2026-01-18
- 3.13.3 — 2026-01-18
- 3.11.1 — 2024-07-15
- 3.11.0 — 2024-07-15
- 3.10.0 — 2024-07-13
- … 64 more at https://npm.io/package/@alterior/di/versions

## README

# @alterior/di

[![Version](https://img.shields.io/npm/v/@alterior/di.svg)](https://www.npmjs.com/package/@alterior/di)

Alterior supports dependency injection using the same patterns as in 
Angular applications. `@alterior/di` is responsible for providing the
underlying dependency injection mechanism.

Injectors created via this library are _heirarchical_. This is an 
important concept which is used throughout the Alterior framework. 
Injectors provide the dependencies specified when they are resolved
and if no matching dependency is found, the request is forwarded 
to the parent injector.

```typescript
import { ReflectiveInjector, Injectable } from '@alterior/di';

@Injectable()
class TestService {
    constructor(readonly date : Date) {
    }

    hello() {
        console.log(`Hello! The time is ${this.date.toString()}`);
    }
}

let injector = ReflectiveInjector.resolveAndCreate([
    { provide: 'Test', useValue: 123 },
    { provide: Date, useValue: new Date() },
    TestService
]);

console.log(injector.get('Test'));
console.log(injector.get(Date));
injector.get(TestService).hello();
```

## Injectors

The `Injector` class is abstract. It has exactly one exposed method:
`get(token : any) : any`. The library ships with `ReflectiveInjector`
which uses Typescript's runtime type reflection (requires `emitDecoratorMetadata`) in order to determine which dependencies are 
requested by a constructor function. Because Typescript only emits 
type metadata for elements which are annotated with a decorator, you 
must decorate any class that participates in DI with the `@Injectable()`
decorator. Since the mere presence of a decorator causes metadata to be 
emitted, you do not need `@Injectable()` if there is any other decorator 
already applied, but it is good practice to include it nonetheless.

## Providers

An Injector is defined by a set of Providers. A Provider specifies an
injection token to be provided as well as the value which should be 
injected when the framework encounters the injection token. 

There are a number of Provider types:
- **Value**: Specify an existing value that should be provided  
  `{ provide: 'TOKEN', useValue: 123 }`

- **Factory**: Define a factory function that will be called in order to create the value that should be provided  
  `{ provide: 'TOKEN', useFactory: () => 123 }`

- **Existing**: Define the provided value by specifying another injection token to resolve  
  `{ provide: 'TOKEN', useExisting: 'ANOTHERTOKEN' }`

- **Class**: The injector will instantiate the given class and use the new instance as the provided value  
  `{ provide: 'TOKEN', useClass: MyClass }`

As a shortcut, you can pass a constructor function (class) without wrapping 
it in a useClass Provider object. Doing so with class `ABC` is the equivalent of specifying `{ provide: ABC, useClass: ABC }`

## Creating Injectors

```typescript
let injector = ReflectiveInjector.resolveAndCreate([ /* providers */ ], parent?);
```

Use `resolveAndCreate()` to create a new injector. If you passed a parent 
injector to `resolveAndCreate()` then the tokens provided by the parent 
injector become available in the new injector (assuming they are not 
overridden).

## Getting Values

Once you've obtained an `Injector` you can use `Injector#get()` to obtain the value for a particular injection token:

```typescript
let instance = injector.get(MyClass);
```

If `MyClass` is not provided by the injector (or any of its parents)
an exception will be thrown. You can change this behavior by passing 
a second parameter to `get()`:

```typescript
let number = injector.get('SomeNumber', 999);
```

Here `number` will be `999` if `injector` does not provide `'SomeNumber'`.
You can use this to avoid throwing an exception if the value is not present:

```typescript
let instance = injector.get(MyClass, null);
```

Here `instance` will be `null` if `injector` does not provide `MyClass`. 
Note: You cannot pass `undefined` here as `get()` will act as if you 
did not pass the second parameter at all. Use `null` instead.

---
_Source: https://npm.io/package/@alterior/di · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
