safe-provide
v2.0.0
Published
A typesafe way to create Angular 2 providers
Downloads
3
Maintainers
Readme
Safe provide
A typesafe way to inject implementations for Typescript interfaces in Angular 2.
Why is this needed?
Angular's allows the user to configure how the injector will instantiate a token. This is commonly used to inject an implementation class when a component requires an interface to be injected. However, Angular does not check that the implementation satisfies the given interface. Consider the following example:
{provide: UserProviderToken, useClass: HttpUserProvider}
Interface information is lost after Typescript compilation. This means
that no check is performed that HttpUserProvider
implements
UserProvider
. The error only shows up in integration tests (or at
runtime if test coverage is poor).
When using safe-provide
's safeProvide
function we get a
compile-time check that HttpUserProvider
implements UserProvider
.
safeProvide(UserProviderToken).useClass(HttpUserProvider);
Usage
In the following example we configure angular to inject an instance of
the HttpUserProvider
class for the UserProvider
interface.
Install package.
$ npm install safe-provide --save
Create a
SafeToken
for your interface.// userProvider.ts import {SafeToken} from "safe-provide"; export const UserProviderToken = new SafeToken<UserProvider>("UserProvider");
Configure a provider using
safeProvide
.// main.ts import {safeProvide, SafeToken} from "safe-provide"; bootstrap(AppComponent, [ safeProvide(UserProviderToken).useClass(HttpUserProvider) ]);
The
safeProvide
function can be used anywhere angular allows provider configuration. Unlike with the Angularprovide
function, compilation will fail if theHttpUserProvider
does not implicitly or explicitly implementUserProvider
. We also have the option of providing a valuesafeProvide(UserProviderToken).useValue(new HttpUserProvider())
or a factory function that returns an
HttpUserProvider
.safeProvide(UserProviderToken).useFactory(userProviderFactory)
Inject the
UserProvider
by passing theSafeToken
to@Inject
.// userComponent.ts @Component({ // configuration }) export class UsersComponent { constructor(@Inject(UserProviderToken) private userProvider:UserProvider) { } }