Skip to content

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

Closed

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

Activity

  1. lacolaco commented on Nov 25, 2024

    @lacolaco
    ContributorAuthor

    The NoInfer type appears to be responsible for the current strange behavior.

    With the current implementation: Exclude<NoInfer<R>, undefined>;

    type ExcludeUndefined<R> = Exclude<NoInfer<R>, undefined>;
    
    let n1: ExcludeUndefined<number | undefined> = undefined;     // ok
    let n2: ExcludeUndefined<Number | undefined> = undefined;     // not excluded
    let s1: ExcludeUndefined<string | undefined> = undefined;     // ok
    let s2: ExcludeUndefined<String | undefined> = undefined;     // not excluded
    let d1: ExcludeUndefined<Date | undefined> = undefined;       // not excluded
    let arr1: ExcludeUndefined<number[] | undefined> = undefined; // not excluded
    let obj1: ExcludeUndefined<{} | undefined> = undefined;       // ok
    let null1: ExcludeUndefined<null | undefined> = undefined;    // ok

    https://www.typescriptlang.org/play/?#code/C4TwDgpgBAogHgYwDYFcAmECqA7DAzAS2wjQB4AlAPigF5ZFUNSA5AewEls8IAnCygDRQUuCIWJpKAbgBQMpBGBRsARgBc9ZOiyjxJUthQBbAEa8oAH2G6iJanRH5baKVDduA9B6isA1vMVlACYNeC0MHCcJFmMzHktrKLtaRLFnV3coL2VWJQgGbTQApQBndU1GHSSyEuAeIgBzBMc0iXtUvRdMrO8-YqgSkIrtSNb9AGU6xuabNpSWzoz3bOxcqHzwkn60crDK0c7SABEAQ2BoKwXndquJJcyVtY3KooUlE54eXYKI2f1DUy8ADaAF0ZtUbn8uo88j8tm8fCYAFbfTYHZykADeAF9wWNJPMofdlr1-AjDEgkKj9lCDChKXjOpDqvdsn0gA

    Without NoInfer:

    type ExcludeUndefined<R> = Exclude<R, undefined>;
    
    let n1: ExcludeUndefined<number | undefined> = undefined;     // ok
    let n2: ExcludeUndefined<Number | undefined> = undefined;     // ok
    let s1: ExcludeUndefined<string | undefined> = undefined;     // ok
    let s2: ExcludeUndefined<String | undefined> = undefined;     // ok
    let d1: ExcludeUndefined<Date | undefined> = undefined;       // ok
    let arr1: ExcludeUndefined<number[] | undefined> = undefined; // ok
    let obj1: ExcludeUndefined<{} | undefined> = undefined;       // ok
    let null1: ExcludeUndefined<null | undefined> = undefined;    // ok

    https://www.typescriptlang.org/play/?#code/C4TwDgpgBAogHgYwDYFcAmECqA7DAzAS2wjQB4AlAPigF5ZFUMKAaKFXCQ4tSgbgCh+SCMCjYAjAC56ydFg5cSpbCgC2AIwgAnKAB82CoiWp12+I2l5Rr1gPS2oAewDWQkWIBM0+LIw5z3KQAcmqaOvpmnBYmBgEkVjZQ9k6uwqIAzlIyjPJxZOnAWkQA5nqxUdwxkYqWiUkOLm4ZXtly-hVKAMqFJWXV0bTlNQk2yY1pUGhZPjntNaQAIgCGwNARhpWD-dwjiWOp7ktaWtMMbRtKKhraANoAun0XPFtPVvtNTuoAVqe+uR1kADeAF9Hnkqq86nYGgdRCokEhfrMnsoUAiwQCIXldu8gA

  2. JeanMeche commented on Nov 25, 2024

    @JeanMeche
    Member

    This looks like an upstream issue with Infer, I've filed microsoft/TypeScript#60594 to track it.

  3. added
    area: core
    core: reactivity
    on Nov 25, 2024
  4. added this to the needsTriage milestone on Nov 25, 2024
  5. samuelfernandez commented on Nov 26, 2024

    @samuelfernandez

    In the meantime, would it be possible to temporarily remove the NoInfer to make sure the loader does not have the undefined type? I feel it is hurting more than helping given this issue exists...

  6. DmitryEfimenko commented on Nov 27, 2024

    @DmitryEfimenko

    switching from Exclude<NoInfer<R>, undefined> to NoInfer<Exclude<R, undefined>> seems to resolve the issue

  7. added a commit that references this issue on Apr 7, 2025
    2b2ab38
  8. added a commit that references this issue on Apr 23, 2025
    d0c9a64
  9. angular-automatic-lock-bot commented on May 24, 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.

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

Metadata

Metadata

Labels

area: corebugcore: reactivitycross-cutting: types

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions