TypeScript中的泛型索引类型值 目标

TypeScript中的泛型索引类型值 目标,typescript,generics,Typescript,Generics,在以下类型定义中将any替换为适当的泛型: 导出类型图形\u操作规范={ 变量:GraphQL_操作变量规范; //离题省略 }; 导出类型图形ql\u操作变量规范={ [参数名称:字符串]:GraphQL_操作变量规范; }; 导出类型图ql\u操作变量规范={ //这里↓ 在这里↓ transformationFunction?:(requestParameter:any)=>any; //这里↓ mustSubmitIf?:(normalizedRequestParameter:an

在以下类型定义中将
any
替换为适当的泛型:

导出类型图形\u操作规范={
变量:GraphQL_操作变量规范;
//离题省略
};
导出类型图形ql\u操作变量规范={
[参数名称:字符串]:GraphQL_操作变量规范;
};
导出类型图ql\u操作变量规范={
//这里↓    在这里↓
transformationFunction?:(requestParameter:any)=>any;
//这里↓
mustSubmitIf?:(normalizedRequestParameter:any)=>布尔值;
//这里↓
transformToNullIf?:(normalizedRequestParameter:any)=>布尔值;
//离题省略
};
例如,将编译以下代码,且不会出现错误:

export const graphQL\u操作规范:graphQL\u操作规范={
变量:{
targetPaginationPageNumber_uuNumerationFrom0:{
//假设API希望页码从0开始,而不是从1开始。
//然而,这里参数和返回值都是数字
transformationFunction(pageNumber\uuu numerationFrom1:number):number{返回pageNumber\uuu numerationFrom1-1;}
},
ItemsCountPerPagination页面:
//假设我们从UI接受字符串,但API需要数字
transformationFunction(StringifiedItemScontPerPaginationPage:string):编号{返回字符串(StringifiedItemScontPerPaginationPage);}
}
}
};
问题 对于
GraphQL\u操作变量规范
,一切都很简单:

导出类型图形ql\u操作变量规范={
transformationFunction?:(requestParameter:RequestParameterForUI)=>RequestParameterForAPI;
mustSubmitIf?:(normalizedRequestParameter:RequestParameterForUI)=>布尔值;
transformToNullIf?:(normalizedRequestParameter:RequestParameterForUI)=>布尔值;
};
问题从这里开始:

export type GraphQL\u OperationVariablesSpecification={
[参数名称:字符串]:GraphQL_操作变量规范;
};
现在
GraphQL\u操作变量规范
也需要两个通用参数!
但我们事先不知道,哪个特定类型将是
RequestParameterForUI
RequestParameterForAPI

如果我们将两个参数添加到
GraphQL\u操作变量规范
GraphQL\u操作规范
也需要它们。但即使我在需要的地方添加通用参数:

导出类型图形\u操作规范={
变量:GraphQL_操作变量规范;
};
导出类型图ql\u操作变量规格={
[参数名称:字符串]:GraphQL_操作变量规范;
};
导出常量图形操作规范:图形操作规范={
变量:{
targetPaginationPageNumber_uuNumerationFrom0:{
transformationFunction(pageNumber\uuu numerationFrom1:number):number{返回pageNumber\uuu numerationFrom1-1;}
},
ItemsCountPerPagination页面:
transformationFunction(StringifiedItemScontPerPaginationPage:string):编号{返回字符串(StringifiedItemScontPerPaginationPage);}
}
}
};
这并不能解决问题:

TS2322: Type '(pageNumber__numerationFrom1: number) => number' is not assignable to
 type '(requestParameter: string | number) => number'.
  Types of parameters 'pageNumber__numerationFrom1' and 'requestParameter' are inco
mpatible.
    Type 'string | number' is not assignable to type 'number'.
      Type 'string' is not assignable to type 'number'.
  > 38 |         transformationFunction(pageNumber__numerationFrom1: number): numbe
r { return pageNumber__numerationFrom1 - 1; }
       |         ^^^^^^^^^^^^^^^^^^^^^^

对于
pageNumber\uu numerationFrom1
,它是数字而不是其他<代码>stringifieditemscontperpagination页面
是字符串,没有其他内容。但是如何向TypeScript解释呢?

我已经修改了您的上一个代码片段,以便它进行类型检查:


但基本上,您需要的不是
GraphQL\u操作规范
,而是
GraphQL\u操作规范

,我认为您在这里遇到了问题,
导出常量GraphQL\u操作规范:GraphQL\u操作规范={
。您可能想做的是使用类型断言来覆盖上面的内容,这将告诉TypeScript,当您说它确实是一个
数字时,请相信您。也许,
转换函数(pageNumber\uu numerationFrom1)作为数字
。为了确保语法的准确性,但我们的想法是重写TypeScript并告诉它,无论你从中得到什么,这都将是一个数字,相信我。但可能仍然有缺点。@Daniel,谢谢你的评论。但是关于信息量,我将开始赏金。