Skip to content

ResourceRef.hasValue() type narrowing not working properly #59707

Closed

Description

@LuisTovar0

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

core

Is this a regression?

No

Description

The hasValue() method of the ResourceRef interface doesn't seem to successfully narrow down the type of its value signal, which is unfortunate since this seems to be hasValue()'s only use.

@Component({
  // ...
  template: `
    @if (res.hasValue()) {
      {{ someMethod(res.value()) }} <!-- error -->
    }
  `,
})
export class AppComponent {
  res: ResourceRef<string>; // created with resource method from @angular/core

  someMethod(str: string) { }
}

In this case, res.value(), despite being inside a context where hasValue() is true, is still considered to have type string | undefined.

Please provide a link to a minimal reproduction of the bug

https://stackblitz.com/edit/stackblitz-starters-wh7weqvh?file=src%2Fapp%2Fapp.component.ts

Please provide the exception or error you saw

Type 'undefined' is not assignable to type 'string'. [plugin angular-compiler]

    src/app/app.component.ts:7:24:
      7 │       {{ someMethod(res.value()) }}

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

Angular CLI: 19.1.1
Node: 22.13.0
Package Manager: npm 11.0.0
OS: win32 x64

Angular: 19.1.1
... animations, cli, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1901.1
@angular-devkit/build-angular   19.1.1
@angular-devkit/core            19.1.1
@angular-devkit/schematics      19.1.1
@angular/cdk                    19.1.0
@angular/material               19.1.0
@schematics/angular             19.1.1
rxjs                            7.8.1
typescript                      5.5.4
zone.js                         0.15.0

Anything else?

This kind of feels like a TypeScript problem and not an Angular problem

Activity

  1. pkozlowski-opensource commented on Jan 24, 2025

    @pkozlowski-opensource
    Member

    This is duplicate of #49161

  2. JeanMeche commented on Jan 24, 2025

    @JeanMeche
    Member

    @pkozlowski-opensource I think this is a different issue, that was already spotted by #58962.

  3. pkozlowski-opensource commented on Jan 24, 2025

    @pkozlowski-opensource
    Member

    @JeanMeche still a duplicate :-)

  4. reopened this on Jan 24, 2025
  5. alxhub commented on Jan 24, 2025

    @alxhub
    Member

    This actually is not a duplicate - the problem is how .hasValue()'s narrowing assertion interacts with subtyping.

    if res is WritableResource<string|undefined> then res.hasValue() narrows it correctly. If res is ResourceRef<string|undefined> then the narrowed type is ResourceRef<string|undefined> & WritableResource<string>, which doesn't narrow the type of .value.

    To fix this, we can add a refinement of the .hasValue() operation to ResourceRef.

  6. added a commit that references this issue on Jan 24, 2025
    000c011
  7. added
    core: reactivity
    on Jan 25, 2025
  8. JeanMeche commented on Jan 25, 2025

    @JeanMeche
    Member

    PR is #59708

  9. added
    area: core
    on Jan 27, 2025
  10. added this to the needsTriage milestone on Jan 27, 2025
  11. added a commit that references this issue on Jan 28, 2025
    6c92d65
  12. added a commit that references this issue on Feb 7, 2025
    011cd21
  13. angular-automatic-lock-bot commented on Feb 28, 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.

  14. locked and limited conversation to collaborators on Feb 28, 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: reactivitystate: has PR

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions