【问题标题】:How to declare a function parameter that takes multiple types in typeScript?如何在 typeScript 中声明一个接受多种类型的函数参数?
【发布时间】:2019-09-26 18:12:20
【问题描述】:

我有一个函数,它将可以是 3 种类型的数组作为其参数之一。该函数使用链中的另外两个函数将该数组作为参数传递给这些函数,并使用该数组的特定索引中的值。该数组是对象数组:

     renderFilters(title: string, filtersArray:Establishments[] | Cuisines[] | Category[] , filterName: string, filterType: string): ReactNode {
            return (
              ...
               filtersArray.map((obj: any, id: number) => {...}) // ERROR:This expression is not callable.
 // Each member of the union type has signatures, but none of those signatures are compatible with each other.
             {this.onHover(id, filtersArray, filterType)}

            )
        }



onHover(id: number, arr:Establishments[] | Cuisines[] | Category[], filterType: string) {
        let obj:Establishments| Cuisines| Category  = arr[id]; // object at index id
        let fArr = arr;
        fArr[id] = this.getnewFilterObj(filterType, obj);
       ...
       this.setState({
                establishmentList: fArr  // ERROR: Types of property  //'establishmentList' are incompatible.
 // Type 'Cuisine[] | Category[] | Establishment[]' is not assignable to //type 'Establishment[]'.
            })
    }

 private getnewFilterObj(type: string, obj: Establishments | Cuisines| Category) {
            switch (type) {
                case 'establishment':
                    return {
                        establishment: {
                            ...obj.establishment, // ERROR: Property //'establishment' does not exist on type 'Cuisine | Category | Establishment'.
  //Property 'establishment' does not exist on type 'Cuisine'.
                            hovered: !obj.establishment.hovered,
                        }
                    }
                case 'category':
                    return {
                        categories: {
                            ...obj.categories,
                            hovered: !obj.categories.hovered,
                        }
                    }
                case 'cuisine':
                    return {
                        cuisine: {
                            ...obj.cuisine,
                            hovered: !obj.cuisine.hovered,
                        }
                    }
                default:
                    return obj;
            }

}

render()函数中

 {this.renderFilters('cuisine', this.state.cuisineList, 'cuisine_name', 'cuisne')}
                                {this.renderFilters('categories', this.state.categoryList, 'name', 'category')}
                                {this.renderFilters('establishment', this.state.establishmentList, 'name', 'establishment')}

【问题讨论】:

    标签: javascript reactjs typescript


    【解决方案1】:

    您应该如下定义数组:

    arr: (Establishments | Cuisines | Category)[]
    

    这意味着数组可能包含所有 3 种类型的项目

    【讨论】:

    • 通过这样做,函数onHover()getNewObject()中的错误仍然存​​在,因为如果我想从对象obj:Establishment | Cuisine | Category访问键,这些键需要在所有接口中都是通用的,这是不可能的因为EstablishmsntsCuisinesCategory 具有所有不同的键。因此,您会看到我面临多个错误。
    • 完美答案,谢谢.. 我对CaseType['participants'] | CaseType[] 有问题必须输入(CaseType['participants'][0] | CaseType)[],您的回答对我有帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-12
    • 1970-01-01
    • 2017-09-28
    • 2022-11-25
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    相关资源
    最近更新 更多