0.7.1 • Published 4 years ago

ts-di-transformer v0.7.1

Weekly downloads
36
License
MIT
Repository
github
Last release
4 years ago

Dependency Injection with Interfaces for TypeScript

npm.io

This is a proof-of-concept of using interfaces with dependency injection. The only requirement for this to work is using a custom transformer.

All the neccessary information is resolved on compile-time. The runtime part get fed the needed information through .bind() and .resolve().

Disclaimer: This has only been tested on TypeScript v3.2.2. Any other version may break this.

How does it work?

The interfaces are converted into symbols when calling InterfaceSymbol<T>() (replace T with the interface). The symbol that's returned will always be the same for each interface.

As this example is not using reflect-metadata it's also resolving the parameters of the constructor. It will automatically append an extra parameter to both .bind() and .resolve() with an array of the parameters.

See https://github.com/YePpHa/ts-di-transformer-example for a proper example of how to use it.

Before custom transformer

interface IBird {
  canFly(): boolean;
}

class Crow implements IBird {
  public canFly() {
    return true;
  }
}

class App {
  constructor(bird: IBird) {
    if (bird.canFly()) {
      console.log("Bird can fly");
    } else {
      console.log("Bird can't fly");
    }
  }
}

const container = new Container();
container.bind(InterfaceSymbol<IBird>(), Crow);

container.resolve(App);

After custom transformer

interface IBird {
  canFly(): boolean;
}

class Crow implements IBird {
  public canFly() {
    return true;
  }
}

class App {
  constructor(bird: IBird) {
    if (bird.canFly()) {
      console.log("Bird can fly");
    } else {
      console.log("Bird can't fly");
    }
  }
}

const container = new Container();
container.bind(Symbol.for("2ef34e55ca621e95ab608501c23d72201fb80158c44cbfe6d3d509d8d2a418d8"), Crow, []);

container.resolve(App, [Symbol.for("2ef34e55ca621e95ab608501c23d72201fb80158c44cbfe6d3d509d8d2a418d8")]);
0.7.1

4 years ago

0.7.0

4 years ago

0.6.0

5 years ago

0.5.3

5 years ago

0.5.2

5 years ago

0.5.1

5 years ago

0.5.0

5 years ago

0.4.0

5 years ago

0.3.0

5 years ago

0.2.4

5 years ago

0.1.4

5 years ago

0.1.3

5 years ago

0.1.2

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago