英文:
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<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.
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<string, any>;
onCategoriesChange: (categories: string[]) => 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[]) => void
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论