We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
core
Actually the providers are not typed. It is easy to make a mistake when you provide something.
It would be nice to write the providers like this.
type AppConfig = { foo: string; bar: number; }; const APP_CONFIG = new InjectionToken<AppConfig>('APP_CONFIG'); @Component({ providers: [provide(APP_CONFIG).useValue({foo: 'abc', bar: 123})] }) const APP_CONFIGS = new InjectionToken<Array<AppConfig>>('APP_CONFIGS'); @Component({ providers: [ provide(APP_CONFIGS).useValue([{foo: 'abc', bar: 123}]), // or provide(APP_CONFIGS, {multi: true}).useValue({foo: 'abc', bar: 123}), ], })
It could be done with something like this.
export type ProviderChoice<T> = { useValue(src: T): ValueProvider; useFactory(src: {(): T}): FactoryProvider; useClass(src: Type<T>): ClassProvider; useExisting(src: ProviderToken<T>): ExistingProvider; }; export type ProvideOptions = { multi?: boolean; }; export function provide<T>(token: ProviderToken<T>): ProviderChoice<T>; export function provide<T>( token: ProviderToken<Array<T>>, options: ProvideOptions & {multi: true}, ): ProviderChoice<T>; export function provide<T>( token: ProviderToken<T>, options: ProvideOptions & {multi: false}, ): ProviderChoice<T>; export function provide<T>( token: ProviderToken<T>, {multi = false}: ProvideOptions = {}, ): ProviderChoice<T> { const options = { provide: token, multi, }; return { useValue: (src) => ({...options, useValue: src}), useFactory: (src) => ({...options, useFactory: src}), useClass: (src) => ({...options, useClass: src}), useExisting: (src) => ({...options, useExisting: src}), }; }
I think the support of deps is not necessary because of the inject function.
deps
inject
There is only one problem with this.
provide(APP_CONFIG, {multi: true});
The type should fail but it does not, because of this.
Alternative is to keep to use any in a provider object.
any
The text was updated successfully, but these errors were encountered:
This is being tracked in #33883.
Sorry, something went wrong.
This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.
Read more about our automatic conversation locking policy.
This action has been performed automatically by a bot.
No branches or pull requests
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
Actually the providers are not typed. It is easy to make a mistake when you provide something.
It would be nice to write the providers like this.
Proposed solution
It could be done with something like this.
I think the support of
deps
is not necessary because of theinject
function.There is only one problem with this.
The type should fail but it does not, because of this.
Alternatives considered
Alternative is to keep to use
any
in a provider object.The text was updated successfully, but these errors were encountered: