Skip to content

resource loader param request fails to exclude undefined from request returned value #58871

@lacolaco

Description

@lacolaco

Which @angular/* package(s) are the source of the bug?

core

Is this a regression?

No

Description

request field in ResourceLoaderParams should exclude undefined type from returned value of request function.

export class ProductViewer {
  productId = input.required<number | undefined>(); 
  productResource: ResourceRef<Product> = resource({
    request: () => this.productId(), // <= number | undefined
    loader: async ({ request: productId, abortSignal }) => {
      assertDefined(productId); // number;
      const resp = await fetch(`https://dummyjson.com/products/${productId}`);
      return resp.json() as Promise<Product>;
    },
  });
}

Source: https://github.com/angular/angular/blob/main/packages/core/src/resource/api.ts#L148-L154

export interface ResourceLoaderParams<R> {
  request: Exclude<NoInfer<R>, undefined>;
  abortSignal: AbortSignal;
  previous: {
    status: ResourceStatus;
  };
}

However, undefined is not excluded for certain types. In my experiments, the behavior is observed when the type is not a primitive like string or number, and can be reproduced by simply changing the type number to Number, for example:

export class ProductViewer {
  productId = input.required<Number | undefined>(); 
  productResource: ResourceRef<Product> = resource({
    request: () => this.productId(), // <= Number | undefined
    loader: async ({ request: productId, abortSignal }) => {
      assertDefined(productId); // !!! undefined remains !!!
      const resp = await fetch(`https://dummyjson.com/products/${productId}`);
      return resp.json() as Promise<Product>;
    },
  });
}

This is a typedef issue, as the request parameter cannot be undefined.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-mp9zqz?file=src%2Fmain.ts

Please provide the exception or error you saw


Please provide the environment you discovered this bug in (run ng version)

Angular CLI: 19.0.0
Node: 18.20.3
Package Manager: npm 10.2.3
OS: linux x64

Angular: 19.0.0
... animations, cli, common, compiler, compiler-cli, core, forms
... platform-browser, router

Anything else?

No response

Metadata

Metadata

Labels

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

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions