Closed
Conversation
When a resource first starts up, even if it transitions immediately to `Loading` it should report a `previous.state` of `Idle`. It was reporting `Loading` as the previous state in such a case because of an oversight in the migration to `linkedSignal` which this commit addresses.
`hasValue` attempts to narrow the type of a resource to exclude `undefined`. Because of the way signal types merge in TS, this only works if the type of the resource is the same general type as `hasValue` asserts. For example, if `res` is `WritableResource<string|undefined>` then `.hasValue()` correctly asserts that `res` is `WritableResource<string>` and `.value()` will be narrowed. If `res` is `ResourceRef<string|undefined>` then that narrowing does _not_ work correctly, since `.hasValue()` will assert `res` is `WritableResource<string>` and TS will combine that for a final type of `ResourceRef<string|undefined> & WritableResource<string>`. The final type of `.value()` then will not narrow. This commit fixes the above problem by adding a `.hasValue()` override to `ResourceRef` which asserts the resource is of type `ResourceRef`. Fixes angular#59707
thePunderWoman
approved these changes
Jan 24, 2025
Contributor
thePunderWoman
left a comment
There was a problem hiding this comment.
reviewed-for: public-api
JeanMeche
reviewed
Jan 24, 2025
| * @experimental | ||
| */ | ||
| export interface ResourceRef<T> extends WritableResource<T> { | ||
| hasValue(): this is ResourceRef<Exclude<T, undefined>>; |
Member
There was a problem hiding this comment.
Should we maybe capture this in a test ?
AndrewKushnir
approved these changes
Jan 24, 2025
Contributor
AndrewKushnir
left a comment
There was a problem hiding this comment.
Reviewed-for: public-api
kirjs
approved these changes
Jan 24, 2025
Member
|
This PR was merged into the repository by commit 6c92d65. The changes were merged into the following branches: main |
pkozlowski-opensource
pushed a commit
that referenced
this pull request
Jan 28, 2025
`hasValue` attempts to narrow the type of a resource to exclude `undefined`. Because of the way signal types merge in TS, this only works if the type of the resource is the same general type as `hasValue` asserts. For example, if `res` is `WritableResource<string|undefined>` then `.hasValue()` correctly asserts that `res` is `WritableResource<string>` and `.value()` will be narrowed. If `res` is `ResourceRef<string|undefined>` then that narrowing does _not_ work correctly, since `.hasValue()` will assert `res` is `WritableResource<string>` and TS will combine that for a final type of `ResourceRef<string|undefined> & WritableResource<string>`. The final type of `.value()` then will not narrow. This commit fixes the above problem by adding a `.hasValue()` override to `ResourceRef` which asserts the resource is of type `ResourceRef`. Fixes #59707 PR Close #59708
PrajaktaB27
pushed a commit
to PrajaktaB27/angular
that referenced
this pull request
Feb 7, 2025
When a resource first starts up, even if it transitions immediately to `Loading` it should report a `previous.state` of `Idle`. It was reporting `Loading` as the previous state in such a case because of an oversight in the migration to `linkedSignal` which this commit addresses. PR Close angular#59708
PrajaktaB27
pushed a commit
to PrajaktaB27/angular
that referenced
this pull request
Feb 7, 2025
`hasValue` attempts to narrow the type of a resource to exclude `undefined`. Because of the way signal types merge in TS, this only works if the type of the resource is the same general type as `hasValue` asserts. For example, if `res` is `WritableResource<string|undefined>` then `.hasValue()` correctly asserts that `res` is `WritableResource<string>` and `.value()` will be narrowed. If `res` is `ResourceRef<string|undefined>` then that narrowing does _not_ work correctly, since `.hasValue()` will assert `res` is `WritableResource<string>` and TS will combine that for a final type of `ResourceRef<string|undefined> & WritableResource<string>`. The final type of `.value()` then will not narrow. This commit fixes the above problem by adding a `.hasValue()` override to `ResourceRef` which asserts the resource is of type `ResourceRef`. Fixes angular#59707 PR Close angular#59708
|
This issue has been automatically locked due to inactivity. Read more about our automatic conversation locking policy. This action has been performed automatically by a bot. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
2 fixes for
resource()