Created
August 19, 2016 20:06
-
-
Save tygern/6e1046151c68d1de838f810231c00f7e to your computer and use it in GitHub Desktop.
Typesafe providers for Angular 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class SafeToken<T> { | |
constructor(private name: string){} | |
toString() { | |
return `Token ${this.name}` | |
} | |
} | |
export function safeProvide<T>(token:SafeToken<T>) { | |
return { | |
useClass<U extends T>(Klass:{new(...args:any[]):U;}) { | |
return {provide: token, useClass: Klass}; | |
}, | |
useValue<U extends T>(value:U) { | |
return {provide: token, useValue: value}; | |
}, | |
useFactory<U extends T>(factory:(...args:any[]) => U) { | |
return {provide: token, useFactory: factory}; | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment