Skip to content

fix(core): fix type inference for Resource#hasValue. - #58962

Closed
BobobUnicorn wants to merge 2 commits into
angular:mainfrom
BobobUnicorn:fix/resource-type-narrowing
Closed

BobobUnicorn wants to merge 2 commits into
angular:mainfrom
BobobUnicorn:fix/resource-type-narrowing

Conversation

@BobobUnicorn

@BobobUnicorn BobobUnicorn commented Nov 28, 2024 •

Copy link
Copy Markdown

TypeScript considers an intersection of two function types to be an overloaded function containing both signatures. The implication of this is that Signal<T|undefined> & Signal<T> is just Signal<T|undefined>, instead of Signal<T> as would be expected. This results in Resource#hasValue not correctly narrowing the type of #value.

This adds an additional two interfaces representing Resources that are known to have a set value, which avoids the function intersection issue.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

This adds an additional two interfaces representing Resources that are known to have a set value.
@angular-robot angular-robot Bot added the area: core Issues related to the framework runtime label Nov 28, 2024
@ngbot ngbot Bot added this to the Backlog milestone Nov 28, 2024
@JeanMeche
JeanMeche marked this pull request as ready for review November 28, 2024 23:39
@flensrocker

Copy link
Copy Markdown

I just come accross this problem, thank you for addressing it!

I fixed it with defining hasValue in this way without introducing a new interface:

hasValue(): this is Omit<Resource<T>, 'value'> & { value: Signal<T> };

I don't know which way is preferred, but just wanted to mention it.

What do you think about that?

@JeanMeche

Copy link
Copy Markdown
Member

With the recent changes we shouldn't need this change anymore.

@JeanMeche JeanMeche closed this Apr 9, 2025
@angular-automatic-lock-bot

Copy link
Copy Markdown

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.

@angular-automatic-lock-bot angular-automatic-lock-bot Bot locked and limited conversation to collaborators May 10, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: core Issues related to the framework runtime cross-cutting: signals

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants