Lambda函数声明中的通用类型数组未编译

时间:2018-11-27 15:06:42

标签: typescript

我正在尝试构建一个通用函数,该函数接收一个回调函数,其参数之一是该通用类型的数组。该方法声明为:

private addPossibleIncludedItems<T extends HasId>(
            jsonApiResponse: any,
            objectList: ObjectTO[],
            typeString: TypeString,
            relationshipIdsGetter: (ObjectTO) => string[],
            multipleItemSetter: (ObjectTO, T[]) => void,
                  // compilation error here ^
            conversionCallBack: (any) => T) {
...
}

我收到以下编译错误:

[ts] ',' expected. [1005]

我似乎找不到该错误的任何原因,因为我已经看到具有泛型类型的声明被用作数组参数声明,所以我很困惑。为什么我的方法声明不能编译?

编辑:没有数组类型的编译就可以了:

private addPossibleIncludedItem<T extends HasId>(
            jsonApiResponse: any,
            objectList: ObjectTO[],
            typeString: TypeString,
            relationshipIdGetter: (ObjectTO) => string,
            singleItemSetter: (ObjectTO, T) => void,
            conversionCallBack: (any) => T) {
// ...
}

1 个答案:

答案 0 :(得分:3)

命名参数之前,先给它们输入类型:

multipleItemSetter: (a: ObjectTO, b: T[]) => void,

这也适用于:

relationshipIdsGetter: (nameMe: ObjectTO) => string[],

...和...

conversionCallBack: (nameMe: any) => T) 

完整示例:

private addPossibleIncludedItems<T extends HasId>(
    jsonApiResponse: any,
    objectList: ObjectTO[],
    typeString: TypeString,
    relationshipIdsGetter: (nameMe: ObjectTO) => string[],
    multipleItemSetter: (a: ObjectTO, b: T[]) => void,
    conversionCallBack: (nameMe: any) => T) {
}