Argument of type 'string[]' is not assignable to parameter of type 'number[]'. Type 'string' is not assignable to type 'number'

huangapple go评论153阅读模式
英文:

Argument of type 'string[]' is not assignable to parameter of type 'number[]'. Type 'string' is not assignable to type 'number'

问题

I made a function to sanitize my category lists. It worked perfectly but I have issues on my terminal.
My code:

onCategories Interface:

interface IEventSearchFilterProps {
  eventData: Record<string, any>;
  // eslint-disable-next-line no-unused-vars
  onCategoriesChange: (categories: number[]) => void;
}
useEffect(() => {

    const sanitizeCategories = () => {

      const filteredCategories = [selectedCategory, selectedLocation, selectedMonth].filter(category => {
        
        return Boolean(category)
      })

      return filteredCategories;
    }

    onCategoriesChange(sanitizeCategories())

  }, [selectedCategory, selectedLocation, selectedMonth])

My Terminal issue:

Argument of type 'string[]' is not assignable to parameter of type 'number[]'.
  Type 'string' is not assignable to type 'number'.
 onCategoriesChange(sanitizeCategories())
                          ~~~~~~~~~~~~~~~~~~~~

With sanitizeCategories underlined.

英文:

I made a function to sanitize my category lists. It worked perfectly but I have issues on my terminal.
My code:

onCategories Interface:

interface IEventSearchFilterProps {
  eventData: Record&lt;string, any&gt;;
  // eslint-disable-next-line no-unused-vars
  onCategoriesChange: (categories: number[]) =&gt; void
}
useEffect(() =&gt; {

    const sanitizeCategories = () =&gt; {

      const filteredCategories = [selectedCategory, selectedLocation, selectedMonth].filter(category =&gt; {
        
        return Boolean(category)
      })

      return filteredCategories;
    }

    onCategoriesChange(sanitizeCategories())

  }, [selectedCategory, selectedLocation, selectedMonth])

My Terminal issue:

Argument of type &#39;string[]&#39; is not assignable to parameter of type &#39;number[]&#39;.
  Type &#39;string&#39; is not assignable to type &#39;number&#39;.
 onCategoriesChange(sanitizeCategories())
                          ~~~~~~~~~~~~~~~~~~~~

With sanitizeCategories underlined.

With sanitizeCategories underlined.

答案1

得分: 2

感谢您编辑问题。

您的控制台上的错误非常清楚,说明出了什么问题。您的函数 sanitizeCategories 返回的是一个字符串数组,而您的函数 onCategoriesChange 期望一个数字数组。

最简单的解决方法是更改 onCategoriesChange 期望的参数类型,如下所示:

interface IEventSearchFilterProps {
  eventData: Record<string, any>;
  onCategoriesChange: (categories: string[]) => void
}

如果出于某种原因您在其他地方使用了 onCategoriesChange 并且它期望一个数字数组,您可以同时接受两种类型:

onCategoriesChange: (categories: number[] | string[]) => void
英文:

Thanks for editing the question.

The error on your console is pretty clear on what is going wrong here.
Your function sanitizeCategories is returning an array of strings, and your function onCategoriesChange is waiting an array of numbers.

The easiest solution is change the type of arguments that onCategoriesChange is waiting, so it will be:

interface IEventSearchFilterProps {
  eventData: Record&lt;string, any&gt;;
  onCategoriesChange: (categories: string[]) =&gt; void
}

If for some reason you are using onCategoriesChange in other place and there is waiting an array of numbers, you can expect both of them:

onCategoriesChange: (categories: number[] | string[]) =&gt; void

huangapple
  • 本文由 发表于 2023年3月4日 01:26:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75630149.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定