TypeScript icon, indicating that this package has built-in type declarations

    3.0.0 • Public • Published


    Simple, typesafe and unobstrusive dependency injection

    Build Status

    Why another dependency injection library for TypeScript?

    I was looking for a dependency injection framework that fulfills those criterias:

    1. It should be very small
    2. It should be unobstrusive (no decorator and metadata voodoo)
    3. It should be easily and intuitively configurable
    4. It should be type-safe
    5. It should have a very low memory footprint
    6. It should be tree-shakeable

    When looking around, I only found libraries like inversify and TypeDI, which are great, I'm sure! But for my needs it was too full-blown and I didn't want to turn on metadata generation, if not really necessary, so I built my own library which matches my requirements.


    typeject is a type safe dependency injection library. All Dependencies in typeject are created lazily via providers. There are two types of providers in this DI framework that you can use as you need.


    Singleton providers always provide the same instance of a type. Once a singleton is instantiated, it's factory can be garbage-collected.


    Volatiles are similar to singletons. The difference is that the factory of each volatile is remembered and the instance is weakly referenced. This means that, if there are no references anymore, the instance is scheduled for garbage collection. If it was garbage collected, the provider will create and serve a new instance on the next request.

    Weak references are only used, if the environment supports it. As a fallback, volatiles are treated as singletons.


    Prototypes always provide a new instance of a factory.

    When to use which type of provider?

    Here are some hints to drive your decision:

    • If your instance of a factory holds state for the lifetime of the app, you should definitely use a singleton for this factory.
    • If memory is a great concern and parts of your app are not used all the time, you should use volatiles. Single Page Apps for example can benefit from this approach.
    • If memory is no concern and you want max performance, go for singletons.
    • If you always need a fresh instance, use a prototype.


    // Types
    interface IServiceA {
      state: { foo: string };
      use(): void;
    interface IServiceB {
      useServiceA(): void;
    // Implementation
    class ServiceA implements IServiceA {
      state = { foo: "bar" };
      use(): void {
    class ServiceB implements IServiceB {
      constructor(private serviceA: IServiceA) {}
      useServiceA() {
    // Configuration
    const serviceA = volatile<IServiceA>(() => new ServiceA());
    const serviceB = volatile<IServiceB>(() => new ServiceB(serviceA()));
    // Run


    singleton<T>(factory: Factory<T>): () => T

    Creates a singleton provider.

    volatile<T>(factory: Factory<T>): () => T

    Creates a volatile provider.


    npm i typeject

    DownloadsWeekly Downloads






    Unpacked Size

    5.97 kB

    Total Files


    Last publish


    • svi3c