You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ResourceStatus of the resource should be string literal instead of enum #58920
Which @angular/* package(s) are relevant/related to the feature request?
core
Description
Enums in TS are not great. Even though there are use-cases where they are appropriate, most of the time string literals do the job better. I believe ResourceStatus should be a string literal.
Keeping ResourceStatus as enum will result in increased boilerplate without any added benefits:
Example using enum:
import{ResourceStatus}from'@angular/core';// this import could be removed with string literals
@Component({template: ` <-- this could be simply comparing to string literal without any lose of strict type checking --/> @if (countriesResource.status() === status.Resolved) { ... } `,})exportclassAppComponent{readonly #http =inject(HttpClient);status=ResourceStatus;// // this property could be removed with string literalscountriesResource=rxResource({loader: ()=>this.#http.get<Country[]>('https://restcountries.com/v3.1/all'),});}
There will be an RFC in the future about resources. That said I agree that this should be either, either through more helper signals like isLoading() or this suggestion. For reference, SolidJS does expose the status as a string rather than an enum and has more helper signals.
Could you provide usecases where the help methods like hasValue(), isLoading() aren't enough in templates ?
My opinion is that you probably won't reach out to the status very often.
With the resource-implementation I had made in user-land before the new resource got into NG19, I liked having the "reload" be available for pagination. This allows to distinguish between adding a global loader indicator, vs. a smaller one at the end of a paginated list. But since there's a high chance that things change with the previous value being retained when the request changes, this is all just hypothetical talk.
(Also yes, I'm aware that currently reload can be extrapolated from combining hasValue() and isLoading().)
@JeanMeche, you're probably right, hasValue(), isLoading() would take care of majority (>50%) use-cases, but I'd argue that's not enough.
in the code I'm experimenting with I reach for status to check on idle and error statuses.
here's a use-case already in the wild where people are reaching for status because isLoading() and hasValue() does not cut it.
Even if there were signals exposed for every possible status, why keep the enum when it can be better? I'd like to hear a compelling argument for an enum.
I'm all for helpers and type narrowing. But is there any reason not to switch from enum to string literal? I don't understand the hesitation
To provide one differing opinion. I always find working with an enum simpler, especially outside the template context. The auto completion for enum works better and I don't need to check and type out the type. This may be because of some properties of the dev environment in Google. In any case, I personally prefer enum slightly.
+1 for string literals, but maybe in uppercase like FormControlStatus.
It also helps on debugging because you see "literal" the status instead of some weird number (which might change if an additional status is added in the future).
When you want to check the status in the template you can write @if (myResource.status() == " and get the autocompletion for all status, and only them, making the discovery and completion simple.
With helper method on ressources you would type @if (myResource. and we would have to find the status methods in all the methods and properties of the resource object.
I find the first one preferable for the dev experience. (and I agree with @flensrocker on the easier debug)
Also I find confusing to have a properties like status and value exposed and having getters like isLoading and hasValue which are doing do nothing more than a simple check and a bool cast. Having methods to replace one-liner is off, especially when learning. It makes you ask yourself "but why ?" and "Is this doing something more ?" while it adds nothing more to the api.
As I understand, the Resource API is only meant for fetching data, because of its "switchMap" behaviour, like cancelling ongoing http-requests, when a new request object is derived from the source signals. And a resource is also bound to the lifecycle of the injection context of the component/service it lives in, so http-request gets cancelled, when the component/service is destroyed.
So IMHO resources should not be used for data mutation, because you usually don't want to cancel the mutation.
But of course that depends on your context/use case. It's doable, but just a bit "out of scope" of the resource. Even for the new httpResource which will land in 19.2.0.
@flensrocker interesting. I'll keep an eye out for the RFC mentioned in the comments above.
I really prefer and appreciate the simplicity of resource in scenarios like disabling a form's submit button while a request is in flight. I hope that the Angular team considers these scenarios as this gets developed or provides similar functionality with the roadmapped signal-based http client.
Anyway, sorry for going off-topic, OP. I still agree that there should be a more convenient way to leverage the status from the template.
Note that resource is intended for read operations, not operations which perform mutations. resource will cancel in-progress loads via the AbortSignal when destroyed or when a new request object becomes available, which could prematurely abort mutations.
Not sure if anyone has mentioned this here regarding this topic - but these statuses are a great use case (in my opinion) for an as const POJO (Plain Old JavaScript Object) that you can then also create a matching type for that exposes some additional type-safety, like an enum. That way you get that same enum behavior but without the enum itself (which has been proven many times by many people to be a part of the TS language that is not ideal both in its construction and runtime behavior behind the scenes).
@johnsonav1992
But then the (integer) values should be hardcoded, right? Or how would a library, which uses this enum behave, if they would change (some new value is inserted in the middle)?
Activity
wartab commentedon Nov 27, 2024
There will be an RFC in the future about resources. That said I agree that this should be either, either through more helper signals like
isLoading()or this suggestion. For reference, SolidJS does expose the status as a string rather than an enum and has more helper signals.https://docs.solidjs.com/reference/basic-reactivity/create-resource
JeanMeche commentedon Nov 27, 2024
Could you provide usecases where the help methods like
hasValue(),isLoading()aren't enough in templates ?My opinion is that you probably won't reach out to the status very often.
wartab commentedon Nov 27, 2024
With the resource-implementation I had made in user-land before the new resource got into NG19, I liked having the "reload" be available for pagination. This allows to distinguish between adding a global loader indicator, vs. a smaller one at the end of a paginated list. But since there's a high chance that things change with the previous value being retained when the request changes, this is all just hypothetical talk.
(Also yes, I'm aware that currently reload can be extrapolated from combining
hasValue()andisLoading().)DmitryEfimenko commentedon Nov 27, 2024
@JeanMeche, you're probably right,
hasValue(),isLoading()would take care of majority (>50%) use-cases, but I'd argue that's not enough.idleanderrorstatuses.isLoading()andhasValue()does not cut it.Even if there were signals exposed for every possible status, why keep the enum when it can be better? I'd like to hear a compelling argument for an enum.
JeanMeche commentedon Nov 27, 2024
The usage case can entirely be addressed by the helpers, and even better the helpers like
hasValuewill allow narrowing.DmitryEfimenko commentedon Nov 28, 2024
I'm all for helpers and type narrowing. But is there any reason not to switch from enum to string literal? I don't understand the hesitation
sbarfurth commentedon Nov 28, 2024
To provide one differing opinion. I always find working with an enum simpler, especially outside the template context. The auto completion for enum works better and I don't need to check and type out the type. This may be because of some properties of the dev environment in Google. In any case, I personally prefer enum slightly.
e-oz commentedon Nov 28, 2024
@sbarfurth autocompletion for
type Example = 'loading' | 'idle'is the same. And you don't need extra import.flensrocker commentedon Dec 6, 2024
+1 for string literals, but maybe in uppercase like
FormControlStatus.It also helps on debugging because you see "literal" the status instead of some weird number (which might change if an additional status is added in the future).
flo-dmtx commentedon Jan 9, 2025
When you want to check the status in the template you can write
@if (myResource.status() == "and get the autocompletion for all status, and only them, making the discovery and completion simple.With helper method on ressources you would type
@if (myResource.and we would have to find the status methods in all the methods and properties of the resource object.I find the first one preferable for the dev experience. (and I agree with @flensrocker on the easier debug)
Also I find confusing to have a properties like
statusandvalueexposed and having getters likeisLoadingandhasValuewhich are doing do nothing more than a simple check and a bool cast. Having methods to replace one-liner is off, especially when learning. It makes you ask yourself "but why ?" and "Is this doing something more ?" while it adds nothing more to the api.ResourceStatusas a union of string literal types #59850 as a duplicate of this issue4 remaining items
flensrocker commentedon Feb 16, 2025
Ah, ok.
As I understand, the Resource API is only meant for fetching data, because of its "switchMap" behaviour, like cancelling ongoing http-requests, when a new request object is derived from the source signals. And a resource is also bound to the lifecycle of the injection context of the component/service it lives in, so http-request gets cancelled, when the component/service is destroyed.
So IMHO resources should not be used for data mutation, because you usually don't want to cancel the mutation.
But of course that depends on your context/use case. It's doable, but just a bit "out of scope" of the resource. Even for the new
httpResourcewhich will land in 19.2.0.dapperdandev commentedon Feb 16, 2025
@flensrocker interesting. I'll keep an eye out for the RFC mentioned in the comments above.
I really prefer and appreciate the simplicity of
resourcein scenarios like disabling a form's submit button while a request is in flight. I hope that the Angular team considers these scenarios as this gets developed or provides similar functionality with the roadmapped signal-based http client.Anyway, sorry for going off-topic, OP. I still agree that there should be a more convenient way to leverage the status from the template.
flensrocker commentedon Feb 16, 2025
@dapperdandev
For reference: https://angular.dev/api/core/resource#description
dapperdandev commentedon Feb 16, 2025
@flensrocker created a separate issue, starting with docs: #59973
johnsonav1992 commentedon Mar 6, 2025
Not sure if anyone has mentioned this here regarding this topic - but these statuses are a great use case (in my opinion) for an
as constPOJO (Plain Old JavaScript Object) that you can then also create a matching type for that exposes some additional type-safety, like an enum. That way you get that same enum behavior but without the enum itself (which has been proven many times by many people to be a part of the TS language that is not ideal both in its construction and runtime behavior behind the scenes).flensrocker commentedon Mar 6, 2025
@johnsonav1992
But then the (integer) values should be hardcoded, right? Or how would a library, which uses this enum behave, if they would change (some new value is inserted in the middle)?
"enum as const" would be some kind of compromise.
johnsonav1992 commentedon Mar 6, 2025
Moving my comments back to the RFC:
#60121 (reply in thread)
sysmat commentedon May 5, 2025
in inline template code complition is lost with
type ResourceStatusJeanMeche commentedon May 5, 2025
This was shipped by #60919 for v20.
sysmat commentedon May 5, 2025
statusEnum: typeof ResourceStatus = ResourceStatus;sysmat commentedon May 5, 2025
rxResource in v 20 is not working with code from ver 19
TS2769: No overload matches this call.JeanMeche commentedon May 5, 2025
@sysmat as it can be expected since
rxResourceis an experimental API.In your case,
loaderhas been renamedstream.angular-automatic-lock-bot commentedon Jun 5, 2025
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.