Skip to content

ResourceStatus of the resource should be string literal instead of enum #58920

Closed

Description

@DmitryEfimenko

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) {
      ...
    }
  `,
})
export class AppComponent {
  readonly #http = inject(HttpClient);
  status = ResourceStatus; // // this property could be removed with string literals
  countriesResource = rxResource({
    loader: () =>
      this.#http.get<Country[]>('https://restcountries.com/v3.1/all'),
  });
}

Example using string literal:

@Component({
  template: `
    @if (countriesResource.status() === 'resolved') {
      ...
    }
  `,
})
export class AppComponent {
  readonly #http = inject(HttpClient);
  countriesResource = rxResource({
    loader: () =>
      this.#http.get<Country[]>('https://restcountries.com/v3.1/all'),
  });
}

There is already confusion about how to check the value of the status in the template here

Proposed solution

export type ResourceStatus =
  | 'idle'
  | 'error'
  | 'loading'
  | 'reloading'
  | 'resolved'
  | 'local';

Alternatives considered

none

Activity

  1. wartab commented on Nov 27, 2024

    @wartab
    Contributor

    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

  2. JeanMeche commented on Nov 27, 2024

    @JeanMeche
    Member

    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.

  3. wartab commented on Nov 27, 2024

    @wartab
    Contributor

    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().)

  4. added
    area: core
    core: reactivity
    on Nov 27, 2024
  5. added this to the needsTriage milestone on Nov 27, 2024
  6. DmitryEfimenko commented on Nov 27, 2024

    @DmitryEfimenko
    Author

    @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.

  7. JeanMeche commented on Nov 27, 2024

    @JeanMeche
    Member

    The usage case can entirely be addressed by the helpers, and even better the helpers like hasValue will allow narrowing.

  8. DmitryEfimenko commented on Nov 28, 2024

    @DmitryEfimenko
    Author

    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

  9. sbarfurth commented on Nov 28, 2024

    @sbarfurth
    Contributor

    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.

  10. e-oz commented on Nov 28, 2024

    @e-oz

    @sbarfurth autocompletion for type Example = 'loading' | 'idle' is the same. And you don't need extra import.

  11. flensrocker commented on Dec 6, 2024

    @flensrocker

    +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).

  12. flo-dmtx commented on Jan 9, 2025

    @flo-dmtx

    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.

  13. 4 remaining items

  14. flensrocker commented on Feb 16, 2025

    @flensrocker

    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 httpResource which will land in 19.2.0.

  15. dapperdandev commented on Feb 16, 2025

    @dapperdandev

    @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.

  16. flensrocker commented on Feb 16, 2025

    @flensrocker

    @dapperdandev

    For reference: https://angular.dev/api/core/resource#description

    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.

  17. dapperdandev commented on Feb 16, 2025

    @dapperdandev

    @flensrocker created a separate issue, starting with docs: #59973

  18. johnsonav1992 commented on Mar 6, 2025

    @johnsonav1992

    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).

  19. flensrocker commented on Mar 6, 2025

    @flensrocker

    @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.

  20. johnsonav1992 commented on Mar 6, 2025

    @johnsonav1992

    Moving my comments back to the RFC:
    #60121 (reply in thread)

  21. sysmat commented on May 5, 2025

    @sysmat

    in inline template code complition is lost with type ResourceStatus

  22. JeanMeche commented on May 5, 2025

    @JeanMeche
    Member

    This was shipped by #60919 for v20.

  23. sysmat commented on May 5, 2025

    @sysmat
    • yes I'm talking in v20 in VSC not working
    • so this will be breaking for users which using like that statusEnum: typeof ResourceStatus = ResourceStatus;
  24. sysmat commented on May 5, 2025

    @sysmat

    rxResource in v 20 is not working with code from ver 19

    postResourceRx = rxResource({
        request: this.id,
        loader: pathId => {
          const id = pathId.request! + 1;
          return this.#postsService.getPost(id + '');
        },
      });
    • error TS2769: No overload matches this call.
    • so ver 20 will have many braking changes
  25. JeanMeche commented on May 5, 2025

    @JeanMeche
    Member

    @sysmat as it can be expected since rxResource is an experimental API.
    In your case, loader has been renamed stream.

  26. angular-automatic-lock-bot commented on Jun 5, 2025

    @angular-automatic-lock-bot

    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.

  27. locked and limited conversation to collaborators on Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: corecore: reactivitycross-cutting: signals

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions