1.0.0 • Published 2 years ago

try-di v1.0.0

Weekly downloads
Last release
2 years ago


Build Status Coverage Status

Try DI is a simple, typesafe and lightweight dependency injection container for TypeScript. Designed with the idea to avoid having runtime errors due to missing, incorrect or outdated mapping configuration.

  • Safe. It type-checks every mapping. Allows to verify all the mappings on CI.
  • Fast. TODO: need to measure a sample
  • Simple. It supports the minimum required and sufficient functionality for most apps.
  • Small. 828 bytes (minified and gzipped). No dependencies. Size Limit controls the size.
import { createContainer } from 'try-di';

const container = createContainer();

  .useValue({ for: Water, use: new Water() })
  .useValue({ for: Fish, use: new Fish() })
  .useFactory({ for: Milk, use: (r) => new Milk(r.resolve(Water)) })
  .useClass({ for: Animal, use: Cat, inject: [Fish, Milk] });

assert.ok(container.resolve(Cat).isCat, "Cat wasn't resolved");

Supports modern browsers, IE and Node.js. Provides modern ES2019 and CommonJS bundles, as well as legacy ones (ES5).

TODO: more samples - how to verifyAll() and all possible ways to register deps