-
Notifications
You must be signed in to change notification settings - Fork 26.9k
Resource fixes #59708
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Resource fixes #59708
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
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
reviewed-for: public-api
| * @experimental | ||
| */ | ||
| export interface ResourceRef<T> extends WritableResource<T> { | ||
| hasValue(): this is ResourceRef<Exclude<T, undefined>>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we maybe capture this in a test ?
AndrewKushnir
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reviewed-for: public-api
|
This PR was merged into the repository by commit 6c92d65. The changes were merged into the following branches: main |
`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
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
`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. |
2 fixes for
resource()