npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@type-injector/lib

v1.0.0-beta.3

Published

inject typescript classes recoursively

Readme

Type Injector Lib

npmjs license build codecov

Use types to get cdi managed instances of objects.

Basics

Inject a simple class

A simple class is a class that has a constructor without arguments (or no constructor at all). This class can get created from the injector without further configuration:

  import { TypeInjector } from '@type-injector/lib';

  /**
   * BaseService without constructor arguments
   * */
  class BaseService {
    readonly isBaseService = true;
  }

  it('should be able to inject a type without further configuration', () => {
    const injector = new TypeInjector();
    const baseService = injector.get(BaseService);
    expect(baseService.isBaseService).to.equal(true);
  });

Inject a class that has constructor dependencies

As type information is lost on runtime and I don't like exprimental type decorators (see Motivation), it is not possible to create a class that has constructor dependencies without any configuration:

  /**
   * NotInjectable with constructor arguments and without config
   * */
  class NotInjectable {
    baseService: BaseService;

    constructor(
      baseService: BaseService
    ) {
      this.baseService = baseService;
    }
  }

The configuration can be placed right inside the class definition:

  /**
   * ComposedService adds an injectConfig to the class with constructor arguments
   * so it gets injectable again
   */
  class ComposedService {
    baseService: BaseService;

    static injectConfig: InjectConfig = {
      deps: [BaseService],
    };
    constructor(
      baseService: BaseService
    ) {
      this.baseService = baseService;
    }
  }

The injectConfig uses the BaseService as a value, so it's preserved on runtime without decorator meta data.

Inject Tokens

Every class that provides an empty constructor or an InjectConfig and Symbols can get used as inject token directly. If you use symbols, you lose type-safty. Therefore you can create inject tokens for everything that is not directly usable as inject token (like simple values or configuration objects or functions):

import { declareInjectToken, TypeInjector } from '@type-injector/lib';

const givenBooleanValue = false;
const tokenForBoolean = declareInjectToken<boolean>('any unique string');

const injector = TypeInjector.construct()
  .provideValue(tokenForBoolean, givenBooleanValue)
.build();

const result = injector.get(tokenForBoolean);

expect(result).to.equal(givenBooleanValue);

Further documentation:

Demo and integration examples:

You can find integration examples for different frameworks (NodeJS, NestJS, Angular, React) in the demo repository:
https://github.com/type-injector/type-injector-lib-demo

Motivation

There are plenty of inject libraries out there. Most of them are part of a larger framework so they are only usable in a browser frontend or a server backend. I'd like to share code between server and client side so I need an inject library that is independant, not part of a large framework and usable in any context.
After I analyzed many of them, the best standalone inject libarary seems to be typed-inject (and in their documentation they list some other awesome injection libraries). It provides compile time dependency checks which is a very strong point esp. for large projects. But as a trade-off you have to configure all indirect dependencies, even simple injection rules - you can't have one without the other.
In most of my use-cases I just want to use one default implementation as inject token and have the possibility to replace it with other alternative implementations. So I decided to do the trade-off the other way around: dropped compile time dependency checks and reduced configuration overhead for the simple use-case.

My Targets

1. Usable anywhere

No dependencies to browser-only or server-only libraries / frameworks.

2. No experimental decorators!

For two reasons:

  • The inject mechanism for a project has to be stable. It's unlikely that typescript will change the decorator mechanism in typescript anymore, but as it's still marked as exprimental, it's possible. If I use an inject library that relies on meta data and decorators it might break with future typescript releases and that might cause a rewrite of the whole project.
  • I do not want to force every project and/or library to publish meta data and therefore increase the size of the compiled output.

3. As little developing overhead as possible

If I use inject for a class without constructor properties just to ensure it uses a single lazy instance of an object accross the whole project I do not like to write any overhead. I don't like to do something like injector.bind('MySimpleService').to(MySimpleService) - I just want to use injector.get(MySimpleService) and the result has to be type-safe. But of course it has to be possible to do something like injector.bind(MySimpleService).to(AlternativeSimpleServiceImpl).

4. Keeping deployment size as small as possible

I won't create dozens of different annotations to do slightly different things and end up with a huge package for basic inject.