API Reference / @dasch-ng/utils / provideFactory
Function: provideFactory()
provideFactory<
T,U>(provide,useFactory,options):FactoryProvider
Defined in: provide-helpers.ts:74
Creates a type-safe factory provider for Angular dependency injection.
This helper ensures that the factory function returns the correct type and that dependencies are properly declared, providing better type safety than plain provider objects.
Type Parameters
T
T
The type that the factory function produces
U
U extends any[] = []
Tuple type of the factory function parameters (inferred from deps)
Parameters
provide
The injection token or class to provide for
InjectionToken<T> | Type<T>
useFactory
(...args) => T
Factory function that creates the value (return type must match T)
options
Configuration object
deps?
U
Optional array of dependencies to inject into the factory function
multi?
boolean
Optional flag to enable multi-provider
Returns
FactoryProvider
A type-safe FactoryProvider object
Example
import { InjectionToken } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { provideFactory } from '@dasch-ng/utils';
interface ApiService {
get(url: string): Observable<any>;
}
const API_SERVICE = new InjectionToken<ApiService>('API_SERVICE');
// Type-safe: factory must return ApiService
provideFactory(
API_SERVICE,
(http: HttpClient) => ({
get: (url: string) => http.get(url)
}),
{ deps: [HttpClient] }
);