Skip to content

Commit

Permalink
refactor(core): remove providers type assertions
Browse files Browse the repository at this point in the history
change provider classifier functions to work as type guards
  • Loading branch information
thiagomini committed Jul 12, 2022
1 parent a2f4b34 commit 4c16d20
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
14 changes: 10 additions & 4 deletions packages/core/injector/helpers/provider-classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = any>(
provider: Provider,
): provider is ClassProvider<T> {
return Boolean((provider as ClassProvider<T>)?.useClass);
}

export function isValueProvider(provider: Provider): boolean {
export function isValueProvider<T = any>(
provider: Provider,
): provider is ValueProvider<T> {
const providerValue = (provider as ValueProvider)?.useValue;
return !isUndefined(providerValue);
}

export function isFactoryProvider(provider: Provider): boolean {
export function isFactoryProvider<T = any>(
provider: Provider,
): provider is FactoryProvider<T> {
return Boolean((provider as FactoryProvider).useFactory);
}
9 changes: 5 additions & 4 deletions packages/core/injector/instance-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,19 +392,20 @@ export class InstanceWrapper<T = any> {
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 || [];
}
}

Expand Down

0 comments on commit 4c16d20

Please sign in to comment.