0

Problem Statement

I am using TypeScript's find() method for an array, but when I input a value, it does not work and says, "No overload matches this call".


Code

addOption(event: MatChipInputEvent) {
    const input = event.input;
    const value = event.value;

    // Makes sure that the same element is not inserted in the the options array
    if (value !== this.options.find(value)) {
      // Add our option
      if ((value || '').trim()) {
        this.options.push(value.trim());
      }
    }

    // Reset the input value
    if (input) {
      input.value = '';
    }
  }

Explanation of Code

As you can see, I am using the find method in the first if condition to check and prevent the same element from coming into the array. The value you see there has a red underline and gives me the error, "No overload matches this call". I seem to be doing everything syntactically correct but the error is still there.


Actual Results

The find method gives me the error, "No overload matches this call" and does not accept the value in the parameters.


Expected Results

The find method to take in the value and find the element in the array.

7
  • 1
    Here's the documentation of find(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…. It doesn't expect a value. It expects a predicate. If you want to test if an array contains a value, you're looking for includes(): developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jan 11, 2020 at 17:59
  • Can you place an answer on how to fix it? Commented Jan 11, 2020 at 18:02
  • How about reading the documentation mentioned in the first comment? It also contains examples on how to use the method. Commented Jan 11, 2020 at 18:03
  • You should be able to answer your own question if you take 2 minutes to read the documentation. Please do. It's an invaluable skill. Commented Jan 11, 2020 at 18:04
  • I was under the assumption that using the find method was the correct way of doing things. I read the find method's doc and was confused because I was doing everything correctly according to it. Commented Jan 11, 2020 at 18:07

1 Answer 1

3

Instead of find, you should use includes.

if (!this.options.includes(value)) {

In case you really want to use find, it can be done as follows:

if (this.options.find(o => o === value) == undefined) {
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.