1

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.

13
  • Try .any instead of .find Commented Oct 9, 2019 at 22:22
  • 1
    You should ensure parentItem is 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. Commented Oct 9, 2019 at 22:26
  • 1
    @JLF, seems like in his definition of find the predicate should return boolean instead of unknown. Commented Oct 9, 2019 at 22:30
  • 1
    @Qwertiy that's gotta be it. Array.prototype.find() for a string array returns a string (or undefined). The find predicate must return a boolean so using the nested urlArr.find() as the return value for the links.find predicate is invalid Commented Oct 9, 2019 at 22:34
  • 1
    @Zacharious means urlArr.some() and not urlArr.any() There's no such thing as .any(). But they're right. .some() is a great idea. Commented Apr 21, 2023 at 17:03

3 Answers 3

3

As noted in the comments, I need to return a boolean in the find function. So doing the following ensure that the result of inner find is converted to a boolean.

const parentItem= this.links.find((link) => {
  // convert to boolean
  return !!(urlArr.find((urlWord) => {
    return urlWord === link.route.split('./')[1]
  }));
});

The code above now returns a matched link object or undefined.

Sign up to request clarification or add additional context in comments.

3 Comments

You should check out Array.prototype.some() as indicated in Qwertiy's answer
Which version of typescript are you using?
"typescript": "~3.2.2",
2

Just update your typescript and/or typings. There is no any error in your code

declare var links: { route: string }[]
declare var urlArr: string[]

const parentItem = links.find(link => urlArr.find(urlWord => urlWord === link.route.split('./')[1]));

as in current version of typescript defines find method as

(method) Array<string>.find(predicate: (value: string, index: number, obj: string[]) => unknown, thisArg?: any): string | undefined (+1 overload)

note unknown instead of boolean - you may return anything you want.

But as you actually don't need result of inner find (unless you can find an empty string), you can use some instead of it.

Comments

0

Facing with same issue by html control disabled="disabled" changed to [disabled]="true" and problem was solved.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.