Open Char2sGu opened 2 months ago
Hi,
We related issues on that topic we #28778 and #51675. Right now, InjectionToken
does not provide info on if the token should be multi
or not.
I see. I think it would still be nice to add provide()
.
This has been on my mind too, as a potential way to add incremental type safety to DI.
Also, š nice issue number!
lol I didn't notice the number.
Do you expect me to create a PR? What am I supposed to do next?
very similar to my proposal.
Definitely would be nice to have such API. But I guess, if we add such API, it has to be nicer than just a type-safe covering function.
I like @SeregPie way of providing tokens or we should simplify the example in this feature-request. I feel it excessive to write token
since if we provide, we provide some token anyway.
rough examples
provide(token: T, options: Omit<Provider, 'provide'>): Provider; // with proper type in options
provideAsValue(token: T, value: TokenValueType, options: { multi: true }): Provider;
provideAsClass(token: T, class: TokenValueType, options: { multi: true }): Provider;
provideAsExisting(token: T, value: TokenValueType, options: { multi: true }): Provider;
provideAsFactory(token: T, factory: () => TokenValueType, options: { multi: true }): Provider;
I feel it more semantical correct to use provideUseValue
or provideFromValue
, since it is the "token" that you are providing.
Besides, I think this syntax might result in not-that-good-looking code formatting results:
Your proposal:
provideUseClass(MyVeryLongClass, () => MyVeryLongClassImplementation, { multi: true });
// can be formmatted into:
provideUseClass(MyVeryLongClass, () => MyVeryLongClassImplementation, {
multi: true,
});
Original proposal:
provideMulti({
token: MyVeryLongClass,
useClass: MyVeryLongClassImplementation,
})
For me, both variants are read kind of the same.
I still don't like having property token
in options. I'd rather deal with formatting since most of the time multi
is used with factory
or value
and with factory
I will have provide
function on a few lines of code anyway.
Anyway, I just want to have this API out of the box.
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
The current object-literal-based providing syntax cannot ensure:
multi: true
option is not missing or mistakenly addedFor libraries, in order to ensure type-safe providing, it has been a common pattern to create
provideXxx
helpers. However, in daily application development, the providng experience is still non-type-safe.Since all the
ProviderToken
s carry type information, it is possible to create framework-level providing helpers to ensure type safety.I have created such helper functions and fully adopted it in all my projects.
I would like to create a PR to introduce these helpers to
@angular/core
to enable a safer providing experience for all Angular developers.Please see below for details.
Proposed solution
Signatures
Usage
Implementation
Alternatives considered
N/A