API Reference / @dasch-ng/utils / src / provideFactory
Function: provideFactory()
provideFactory<
T,U>(provide,useFactory,options?):FactoryProvider
Defined in: libs/ng/utils/src/lib/provide-helpers.ts:101
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 unknown[] = []
Tuple type of the factory function parameters (inferred from useFactory)
Parameters
provide
InjectionToken<T> | Type<T>
The injection token or class to provide for
useFactory
(...args) => T
Factory function that creates the value (return type must match T)
options?
Configuration object
deps?
NoInfer<FactoryDeps<U>>
Optional array of DI tokens whose values are passed to the factory, one token per factory parameter. Each token must provide the type of its parameter.
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] }
);