UNPKG

ioc-check

Version:

Runtime checks for error free dependency injection.

27 lines 1.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DependencyInjectionError = exports.throwIfMatches = void 0; const DependencyInjectionError_1 = require("./errors/DependencyInjectionError"); Object.defineProperty(exports, "DependencyInjectionError", { enumerable: true, get: function () { return DependencyInjectionError_1.DependencyInjectionError; } }); /** * Throws an error if the instance is an instance of the class and not of a subclass. * Will not produce runtime errors for instances and classes that do not match. * @param instance The instance to check. * @param constructor The class to match. * @example Will not throw: * class A {} * throwIfMatches(new A(), Object); * @example Will throw: * class A{} * throwIfMatches(new A(), A); * @typeParam Type The actual type (prototype) of the class and class instance. */ function throwIfMatches(instance, constructor) { // Compare the prototypes if the instance and the class. // If they match exactly it is a direct instantiation. if (Object.getPrototypeOf(instance) === constructor.prototype) { throw new DependencyInjectionError_1.DependencyInjectionError(constructor); } } exports.throwIfMatches = throwIfMatches; //# sourceMappingURL=throwIfMatches.js.map