From 4143afbfb2f75e3b21207e809b62a40d429608c2 Mon Sep 17 00:00:00 2001 From: Nikita Sobolev Date: Thu, 30 Jul 2020 02:56:49 +0300 Subject: [PATCH] fix: use typeof operator to check function type (#100) --- src/ContainerInstance.ts | 10 +++++----- src/decorators/Service.ts | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ContainerInstance.ts b/src/ContainerInstance.ts index e0212871..9d7c88eb 100644 --- a/src/ContainerInstance.ts +++ b/src/ContainerInstance.ts @@ -183,7 +183,7 @@ export class ContainerInstance { if (typeof identifierOrServiceMetadata === "object" && (identifierOrServiceMetadata as { service: Token }).service) { return this.set({ id: (identifierOrServiceMetadata as { service: Token }).service, value: value }); } - if (identifierOrServiceMetadata instanceof Function) { + if (typeof identifierOrServiceMetadata === "function") { return this.set({ type: identifierOrServiceMetadata, id: identifierOrServiceMetadata, value: value }); } @@ -231,7 +231,7 @@ export class ContainerInstance { if (service.id) return service.id === identifier; - if (service.type && identifier instanceof Function) + if (service.type && typeof identifier === "function") return service.type === identifier || identifier.prototype instanceof service.type; return false; @@ -253,7 +253,7 @@ export class ContainerInstance { return service.id === identifier; } - if (service.type && identifier instanceof Function) + if (service.type && typeof identifier === "function") return service.type === identifier; // todo: not sure why it was here || identifier.prototype instanceof service.type; return false; @@ -281,10 +281,10 @@ export class ContainerInstance { if (service && service.type) { type = service.type; - } else if (service && service.id instanceof Function) { + } else if (service && typeof service.id === "function") { type = service.id; - } else if (identifier instanceof Function) { + } else if (typeof identifier === "function") { type = identifier; // } else if (identifier instanceof Object && (identifier as { service: Token }).service instanceof Token) { diff --git a/src/decorators/Service.ts b/src/decorators/Service.ts index dd57b32a..f4ea40ad 100644 --- a/src/decorators/Service.ts +++ b/src/decorators/Service.ts @@ -75,7 +75,7 @@ export function Service(options?: ServiceOptions): F * Marks class as a service that can be injected using container. */ export function Service(optionsOrServiceName?: ServiceOptions|Token|string|any[]|(() => any), maybeFactory?: (...args: any[]) => any): any { - if (arguments.length === 2 || (optionsOrServiceName instanceof Function)) { + if (arguments.length === 2 || typeof optionsOrServiceName === "function") { const serviceId = { service: new Token() }; const dependencies = arguments.length === 2 ? optionsOrServiceName as any[] : []; const factory = arguments.length === 2 ? maybeFactory : optionsOrServiceName as Function;