UNPKG

@michaelganchas/ts-inject

Version:

A lightweight TypeScript dependency injection library

28 lines (27 loc) 1.09 kB
"use strict"; var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) { if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) { if (ar || !(i in from)) { if (!ar) ar = Array.prototype.slice.call(from, 0, i); ar[i] = from[i]; } } return to.concat(ar || Array.prototype.slice.call(from)); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.autowire = void 0; var container_1 = require("./container"); /** * Autowire a class constructor with resolved dependencies. * * @param ClassDef - The target class constructor * @param keys - Keys of dependencies to inject from DI container * @return A function that returns a new instance of the class with injected dependencies */ var autowire = function (ClassDef, keys) { return function () { var deps = keys.map(function (key) { return container_1.container.get(key); }); return new (ClassDef.bind.apply(ClassDef, __spreadArray([void 0], deps, false)))(); }; }; exports.autowire = autowire;