Issue
I have a code in which the memberships$ is an observable of objects that have a property "role". I want to use reduce function in order to traverse all of them, and if anyone's role is "Collector" I want to return value true. This is the code:
hasCollectorRole$: Observable<boolean> = this.memberships$.pipe(
map(arr => {
return arr.reduce((acc, val) => {
if (val.role == "Collector") {
acc = true;
}
return acc;
}, false)
})
);
This is the error that I get on it:
Type 'true' is not assignable to type 'false'.
How do I fix this?
Solution
Try specifying the reducer's default value's type via:
arr.reduce(..., false as boolean)
Right now, it looks like TS thinks the type that should be returned is false, not boolean, and true is not assignable to type false.
Answered By - D M
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.