Compiler v2
Compiler v2

Reputation: 3605

TypeScript array find() method saying, "No overload matches this call"

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.

Upvotes: 0

Views: 1301

Answers (1)

uminder
uminder

Reputation: 26150

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) {

Upvotes: 3

Related Questions