Repository navigation
Closed
Labels
area: coreIssues related to the framework runtimecore: reactivityWork related to fine-grained reactivity in the core frameworkstate: has PR
Milestone
Description
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
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
Metadata
Metadata
Assignees
Labels
area: coreIssues related to the framework runtimecore: reactivityWork related to fine-grained reactivity in the core frameworkstate: has PR
Activity
pkozlowski-opensource commentedon Jan 24, 2025
This is duplicate of #49161
JeanMeche commentedon Jan 24, 2025
@pkozlowski-opensource I think this is a different issue, that was already spotted by #58962.
pkozlowski-opensource commentedon Jan 24, 2025
@JeanMeche still a duplicate :-)
alxhub commentedon Jan 24, 2025
This actually is not a duplicate - the problem is how
.hasValue()'s narrowing assertion interacts with subtyping.if
resisWritableResource<string|undefined>thenres.hasValue()narrows it correctly. IfresisResourceRef<string|undefined>then the narrowed type isResourceRef<string|undefined> & WritableResource<string>, which doesn't narrow the type of.value.To fix this, we can add a refinement of the
.hasValue()operation toResourceRef.fix(core): add `hasValue` narrowing to `ResourceRef`
JeanMeche commentedon Jan 25, 2025
PR is #59708
fix(core): add `hasValue` narrowing to `ResourceRef` (angular#59708)
fix(core): add `hasValue` narrowing to `ResourceRef` (angular#59708)
angular-automatic-lock-bot commentedon Feb 28, 2025
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.