Skip to content

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

@DmitryEfimenko

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: coreIssues related to the framework runtimecore: reactivityWork related to fine-grained reactivity in the core frameworkcross-cutting: signals

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions