如何修复TypeScript中的“No Overload Matches This Call”错误?SelectList onSelect属性

hi3rlvi2  于 7个月前  发布在  TypeScript
关注(0)|答案(1)|浏览(124)

我目前正在一个团队中开发一个React Native应用程序。我一直试图在应用程序中的一个页面上实现一个SelectList,该页面调用一个SelectSelect函数,该函数接受选定的值并将其传递给选择时的函数。我的团队最近决定转向TypeScript文件而不是JavaScript文件。SelectList在Javascript版本中工作,但现在我们切换到Typescript onSelect属性的SelectList抛出了一个“No Overload Matches This Call”错误,我不确定该怎么办。该函数应该过滤一个全局列表,然后该列表将显示在页面的更下方。
完整的错误如下:
没有重载与此调用匹配。重载% 1/ % 2,'(props:SelectListProps|只读):SelectList“,给出了以下错误。键入”(selected:any)=> void "不可分配给类型“()=> void”。目标签名提供的参数太少。应为1或更多,但得到0。重载2(共2个),“(props:SelectListProps,context:any):SelectList ',给出了以下错误。(selected:any)=> void' is not assignable to type '()=> void '. ts(2769)index.d.ts(79,5):期望的类型来自属性'onSelect',该属性在类型'IntrinsicAttributes & IntrinsicClassAttributes & Readonly'上声明index.d.ts(79,5):预期的类型来自属性“onSelect”,该属性在此处对类型“IntrinsicAttributes & IntrinsicClassAttributes & Readonly”声明
我的SelectList代码如下:

import { SelectList } from 'react-native-dropdown-select-list';
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
const [selected, setSelected] = React.useState("");

export default function ClientAp(){

const filterOption = [
        {key: 'All', value: 'All'},
        {key: 'Today', value: 'Today'},
        {key: 'This Week', value: 'This Week'},
        {key: 'This Month', value: 'This Month'}
    ]

const handleSelection  = (key) => {
        if(key == 'All Upcoming')
        {
            //filter by upcoming appointments
        }
        else if(key == 'Today')
        {
            //filter by appointments today
        }
        else if(key == 'This Week')
        {
            //filter by appointments this week
        }
        else if(key == "This Month")
        {
           //filter by appointments this month
        }

}

return(
    <>
       {/* more stuff up here*/}

       <View style = {styles.dropdowncont}>
                <SelectList
                    setSelected = {setSelected}
                    data = {filterOption}
                    maxHeight = {300}
                    boxStyles = {{backgroundColor:'white'}}
                    dropdownStyles = {{backgroundColor:'white'}}
                    search = {false}
                    defaultOption = {{key: 'All', value: 'All'}}
                    onSelect = {(selected) => handleSelection(selected)} {/* error on this line */}
                />
        </View>

        {/* more stuff down here */}

    </>
);
}
const styles = StyleSheet.create({
        dropdowncont: {
        padding: 16
}

字符串
我试过在selectable selection函数中添加一个返回值,但它什么也不返回。我也试过在选定的变量中添加一个变量类型,就像这样:(selected:string)。我一直在尝试更改所有内容的类型。尽管如此,在修复这个错误方面没有任何进展。
如果需要更多的代码来解决这个问题,我很乐意效劳。任何帮助都将不胜感激!

sqougxex

sqougxex1#

onSelect不像错误所说的那样有任何参数。

Type '(selected: any) => void' is not assignable to type '() => void

字符串
setSelected中使用handleSelection,类似这样:

const handleSelection  = (key) => {
    setSelected(key)
    if(key == 'All Upcoming')
...

}

...

<SelectList
    setSelected = {handleSelection}
    data = {filterOption}    
    maxHeight = {300}
    boxStyles = {{backgroundColor:'white'}}
    dropdownStyles = {{backgroundColor:'white'}}
    search = {false}
    defaultOption = {{key: 'All', value: 'All'}}
/>

相关问题