From 4c16d2010c7172164a3be333c635c6f47dec933a Mon Sep 17 00:00:00 2001 From: Thiago Martins Date: Tue, 12 Jul 2022 10:03:40 -0300 Subject: [PATCH] refactor(core): remove providers type assertions change provider classifier functions to work as type guards --- .../core/injector/helpers/provider-classifier.ts | 14 ++++++++++---- packages/core/injector/instance-wrapper.ts | 9 +++++---- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/core/injector/helpers/provider-classifier.ts b/packages/core/injector/helpers/provider-classifier.ts index ab8264dbbd6..b883a4ea83b 100644 --- a/packages/core/injector/helpers/provider-classifier.ts +++ b/packages/core/injector/helpers/provider-classifier.ts @@ -6,15 +6,21 @@ import { } from '@nestjs/common'; import { isUndefined } from '@nestjs/common/utils/shared.utils'; -export function isClassProvider(provider: Provider): boolean { - return Boolean((provider as ClassProvider)?.useClass); +export function isClassProvider( + provider: Provider, +): provider is ClassProvider { + return Boolean((provider as ClassProvider)?.useClass); } -export function isValueProvider(provider: Provider): boolean { +export function isValueProvider( + provider: Provider, +): provider is ValueProvider { const providerValue = (provider as ValueProvider)?.useValue; return !isUndefined(providerValue); } -export function isFactoryProvider(provider: Provider): boolean { +export function isFactoryProvider( + provider: Provider, +): provider is FactoryProvider { return Boolean((provider as FactoryProvider).useFactory); } diff --git a/packages/core/injector/instance-wrapper.ts b/packages/core/injector/instance-wrapper.ts index 7fb59a6ad24..773a9b4f50b 100644 --- a/packages/core/injector/instance-wrapper.ts +++ b/packages/core/injector/instance-wrapper.ts @@ -392,19 +392,20 @@ export class InstanceWrapper { if (isValueProvider(provider)) { this.metatype = null; this.inject = null; + this.scope = Scope.DEFAULT; this.setInstanceByContextId(STATIC_CONTEXT, { - instance: (provider as ValueProvider).useValue, + instance: provider.useValue, isResolved: true, isPending: false, }); } else if (isClassProvider(provider)) { this.inject = null; - this.metatype = (provider as ClassProvider).useClass; + this.metatype = provider.useClass; } else if (isFactoryProvider(provider)) { - this.metatype = (provider as FactoryProvider).useFactory; - this.inject = (provider as FactoryProvider).inject || []; + this.metatype = provider.useFactory; + this.inject = provider.inject || []; } }