I have following typescript code:
const parentItem= this.links.find((link) => {
return urlArr.find((urlWord) => {
return !!(urlWord === link.route.split('./')[1])
})
});
// link.route.split('./')[1] is a string
// urlWord is also a string
// urlArr is array of strings
// links is an array of objects
and typescript keeps on complaining:
.component.ts(11,59): error TS2322: Type 'string' is not assignable to type 'boolean'.
The code works fine in chrome console.
This may look like similar to this question but even after reading it I couldn't figure out where the problem is.
parentItemis receives a string:const parentItem: string = .... It may be worth actually adding expected types throughout your code as you'll get a more helpful error much closer to the actual issue. It's hard to tell what's going on here because none of us know what actual types have been laid out in the system. Typescript is about making less assumptions. You're asking us to make a bunch of assumptions here.findthepredicateshould returnbooleaninstead ofunknown.Array.prototype.find()for a string array returns a string (or undefined). Thefindpredicate must return a boolean so using the nestedurlArr.find()as the return value for thelinks.findpredicate is invalidurlArr.some()and noturlArr.any()There's no such thing as.any(). But they're right..some()is a great idea.