node.js - 如何将打字稿泛型用于动态函数参数
问题描述
我正在尝试围绕节点的 gRPC bindings创建一个包装器方法。我想创建一个调用的方法,该方法调用底层类的方法,但还要对方法和请求参数进行类型检查。rpc
WrapperClient
GrpcClient
这是我将交叉发布到TS 游乐场的示例。
type ReqA = { type: 'a' }
type ReqB = { type: 'b' }
class GrpcClient {
findA(request: ReqA) { };
findB(request: ReqB) { };
}
class WrapperClient {
rpc<GrpcClient, TMethod extends keyof GrpcClient>(client: GrpcClient, method: TMethod, req: any) {
}
}
const grpcClient = new GrpcClient()
const client = new WrapperClient()
// This works
grpcClient.findA({ type: 'a' }) // correct
grpcClient.findB({ type: 'b' }) // correct
// This doesn't.
// It Matches the method name. That's good.
// But it does not check the request type.
client.rpc(grpcClient, 'findA', 1) // should fail
client.rpc(grpcClient, 'findB', 1) // should fail
client.rpc(grpcClient, 'findC', 1) // double fail, the method check works though
我可以使用extends keyof
泛型表达式对方法名称进行类型检查。我无法输入检查请求参数。
我可以将联合硬编码为请求参数类型。
rpc<GrpcClient, TMethod extends keyof GrpcClient>(client: GrpcClient, method: TMethod, req: ReqA | ReqB) {
gRPC 绑定是动态生成的,我不想维护在重新生成绑定时可能更改的请求类型列表。
想法?
解决方案
您可以使用条件类型来确定请求类型:
type ReqA = { type: 'a' }
type ReqB = { type: 'b' }
class PeopleServiceClient {
findA(request: ReqA) { };
findB(request: ReqB) { };
}
class WrapperClient {
rpc<PeopleServiceClient, TMethod extends keyof PeopleServiceClient>(
client: PeopleServiceClient, method: TMethod,
req: PeopleServiceClient[TMethod] extends (arg: infer T) => void ? T : never) {
}
}
const grpcClient = new PeopleServiceClient()
const client = new WrapperClient()
grpcClient.findA({ type: 'a' }) // correct
grpcClient.findB({ type: 'b' }) // correct
client.rpc(grpcClient, 'findA', {type: 'a'}) // correct
client.rpc(grpcClient, 'findA', {type: 'b'}) // fails
client.rpc(grpcClient, 'findA', 1) // fails
client.rpc(grpcClient, 'findB', 1) // fails
client.rpc(grpcClient, 'findC', 1) // fails
推荐阅读
- python - python类中的方法重命名有什么作用?
- javascript - “无法读取未定义的属性'sendMessage'”在一个调用中被抛出,但在另一个调用中没有抛出(而且它们几乎彼此相邻)
- python - 在 tensorflow 中使用 import_meta_graph 时使用动态 input_map
- r - 条形图可视化的重复代码
- c# - 为什么 Visual Studio Code 没有针对 Unity 关键字(例如 transform 或 Vector3)的智能感知?
- python - 如何从大量(4.5 M)文件中提取特定行并正确调试?
- google-sheets - 自动更新导出
- sql - 在需要字符串文字的情况下使用 SQL 参数
- nginx - Nginx上游服务器过早关闭连接
- amazon-web-services - 从 python 获取 boto3 配置文件设置