我有这样的 proto(3) 消息:
message DataAggregated {
repeated Data data = 1;
}
message Data {
string example = 1
}
当为这个原型定义生成类型时,“列表”这个词被附加到重复的字段中,为什么会这样,有正当的理由吗?有没有办法阻止这个附加?这会引起巨大的头痛。
export namespace DataAggregated {
export type AsObject = {
dataList: Array<Data.AsObject>,
}
}
问题
注意:我在类实例上使用 AsObject 的原因很复杂,无法在这里解释,并且超出了这个问题的范围
这不会通过 GRPC 返回任何内容
// I cannot use a return interface here as dataList is not part of the underlying
// GRPC message so i must return data
get() {
return {
dataList: ['string', 'string']
}
}
这将返回数据
// I cannot use a return interface here as data does not exist in the interface
get() {
return {
data: ['string', 'string']
}
}
从另一台服务器调用函数时
export interface ServiceInterface {
get(data: Empty, metadata: Metadata): Observable< DataAggregated.AsObject>;
}
....
const res = get();
console.log(res.data) // TsErr: Property 'data' does not exist on type 'AsObject'.ts(2339)
console.log(res.dataList) // undefined
如果您不选择使用这些类,这会使重复字段的类型变得无用。为什么在 GRPC 需要数据而不是 dataList 时附加列表这似乎是一种反模式